Multiply Matrices In Java ISC

Program to multiply two given matrices.

class Multiplymatrices{
//start of class
public void accept(int a[][],int b[][]){
int r1=a.length;
int r2=b.length;
int c1=a[0].length;
int c2=b[0].length;
int c[][]=new int[r1][c2];
if(r2==c1){
for(int i=0;i<r1;i++){
for(int j=0;j<c2;j++){
for(int k=0;k<c1;k++){
c[i][j]+=a[i][k]*b[k][j];
}}}
System.out.println("the new matrix");
for(int i=0;i<r1;i++){
for(int j=0;j<c2;j++){
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
else
{
System.out.println("Wrong input");
System.exit(1);
}}}//end of class

OUTPUT---
Enter the value of r1 then c1 and r2 and c2
2
2
2
2
Enter any number
4
Enter any number
3
Enter any number
4
Enter any number
3
Enter any number
4
Enter any number
3
Enter any number
4
Enter any number
3
2821
2821

Comments

Popular posts from this blog

Sort Boundary Elements Of A Matrix

Lucky Number In Java ISC

Program to fill a square matrix of size ‘n*n” in a circular fashion (clockwise) with natural numbers from 1 to n*n, taking ‘n’ as input.