Multiplication of Two Matrices

Aim: 
To write a java program to multiply two given matrices.

Description:
If the order of matrix A is r1 x c1 and of matrix B is r2 x c2 (number of columns of A = number of rows of B = c1=r2), then the order of matrix C is r1 x c2, where C = A x B otherwise matrix multiplication is not possible. First accept number of rows and columns of matrix A into r1, c1 then accept number of rows and columns of matrix B into r2, c2. If c1 is not equal to r2 then display a message matrix multiplication is not available otherwise accept two matrices into two arrays and perform the multiplication operation and store the result in another array and display the same as result.

Program:
import java.io.*;
class MatMul
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(
System.in));
System.out.println("Enter the row and columns for matrix A:");
int r1=Integer.parseInt(br.readLine());
int c1=Integer.parseInt(br.readLine());
System.out.println("Enter the row and columns for matrix B:");
int r2=Integer.parseInt(br.readLine());
int c2=Integer.parseInt(br.readLine());
int i, j, k;
int a[ ][ ]=new int[r1][c1];
int b[ ][ ]=new int[r2][c2];
int c[ ][ ]=new int[r1][c2];
if(c1!=r2)
System.out.println("Matrix multiplication is not possible");
else
{
System.out.println("Enter the elements for matrixA:");
for(i=0;i<r1;i++)
{ for(j=0;j<c1;j++)
{
a[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("Enter the elements for matrix B:");
for(i=0;i<r2;i++)
{ for(j=0;j<c2;j++)
{
b[i][j]=Integer.parseInt(br.readLine());
}
}
//matrix multiplication
for(i=0;i<r1;i++)
{ for(j=0;j<c2;j++)
{ c[i][j]=0;
for(k=0;k<c1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
}
System.out.println("Resultant matrix is:");
for(i=0;i<r1;i++)
{ System.out.println();
for(j=0;j<c2;j++)
{
System.out.print(c[i][j]+"\t");
}
}
}
}