/*WAP of Matrix Multiplication in Java*/
import java.lang.*; import java.io.*; import java.util.*; class matrix { public static void main (String args[]) throws IOException { int a[][] = new int[2][2]; int b[][] = new int[2][2]; int c[][] = new int[2][2]; Scanner sc = new Scanner(System.in); System.out.println("Enter Elements of Matrix 1"); for(int i=0; i<2; i++) { for(int j=0; j<2; j++) { a[i][j] = sc.nextInt(); } } System.out.println("Enter Elements of Matrix 2"); for(int i=0; i<2; i++) { for(int j=0; j<2; j++) { b[i][j] = sc.nextInt(); } } // calculation for(int i=0; i<2; i++) { for(int j=0; j<2; j++) { for(int k=0; k<2; k++) { c[i][j] += a[i][k] * b[k][j]; } } } System.out.println("Matrix After multiplication"); for(int i=0; i<2; i++) { for(int j=0; j<2; j++) { System.out.println( "\t" + c[i][j] ); } } } }
No comments:
Post a Comment