how to code Matrix Program in java

file name - MatrixProg.java


import java.util.*;
class MatrixProg
{
public static void main(String[] args) {


int r,c,r1,c1,r2,c2, k;
Scanner ob=new Scanner(System.in);


System.out.println("Enter the order of 1st matrix");
r1=ob.nextInt();
c1=ob.nextInt();

System.out.println("Enter the order of 2st matrix");
r2=ob.nextInt();
c2=ob.nextInt();

if(c1!=r2)
{
System.out.println("multiplication is not possible ");
}
else
{
int arrA[][]=new int[r1][c1];
int arrB[][]=new int[r2][c2];
int arrp[][]=new int[r1][c2];

System.out.println("Enter the element of 1st matrix");
for(r=0;r<r1;r++)
{
for (c=0;c<c1 ;c++ )
{
arrA[r][c]=ob.nextInt();
}
}
System.out.println("Enter the element of 2nd matrix");
for(r=0;r<r2;r++)
{
for (c=0;c<c2 ;c++ )
{
arrB[r][c]=ob.nextInt();
}
}

System.out.println("multiplication Process");

for (r=0;r<r1 ;r++ )
 {
for (c=0;c<c2 ;c++ ) {

arrp[r][c]=0;
for (k=0;k<c1 ;k++)
 {

arrp[r][c]=arrp[r][c]+arrA[r][k]*arrB[k][c];
}

}
}

for (r=0;r<r1 ;r++ ) {
for (c=0;c<c1 ;c++) {

System.out.print(arrp[r][c]+"\t");
}
System.out.println( );
}





}

}
}

No comments:

INTRODUCTION TO COMPUTER NETWORKS

A Computer network consists of two or more autonomous computers that are linked (connected) together in order to: • Share resources (files...