import java.io.*;
import java.util.*;
public class Matrix
{
public static void main(String a[]) throws Exception
{
int r;
int c;
int r1;
int c1;
int[][] matrix_A;
int[][] matrix_B;
int[][] matrix_ans;
Scanner cin=new Scanner(System.in);
System.out.println("Enter the number of rows in matrix A : ");
r=cin.nextInt();
System.out.println("Enter the number of columns in matrix A : ");
c=cin.nextInt();
System.out.println("Enter the number of rows in matrix B : ");
r1= cin.nextInt();
System.out.println("Enter the number of columns in matrix B : ");
c1=cin.nextInt();
if(c==r1)
{
matrix_A=new int[r][c];
matrix_B=new int[r1][c1];
matrix_ans=new int[r][c1];
int x,y;
x=matrix_A.length;
y=matrix_B.length;
System.out.println("Enter the elements of matrix A : ");
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
{
matrix_A[i][j]=cin.nextInt();
}
System.out.println("Enter the elements of matrix B : ");
for(int i=0;i<r1;i++)
for(int j=0;j<c1;j++)
{
matrix_B[i][j]=cin.nextInt();
}
for(int i = 0; i < x; i++)
{
for(int j = 0; j < y; j++)
{
for(int k = 0; k < y; k++)
{
matrix_ans[i][j] += matrix_A[i][k]*matrix_B[k][j];
}
}
}
for(int i = 0; i < x; i++)
{
for(int j = 0; j < y; j++)
{
System.out.print(" "+matrix_ans[i][j]);
}
System.out.println();
}
}
else
{
System.out.println("Cant multiply");
}
}
}
No comments:
Post a Comment