In this post, we will see how to multiply two matrices in java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Scanner; public class MatrixOperator { public static void main(String args[]) { int r1, c1, r2, c2, sum = 0, i, j, k; Scanner in = new Scanner(System.in); System.out.print("Enter number of rows and columns of first matrix : "); r1 = in.nextInt(); c1 = in.nextInt(); int first[][] = new int[r1][c1]; System.out.print("Enter first matrix elements : "); for(i=0 ; i<r1 i for j first in.nextint system.out.print number of rows and columns second matrix : r2="in.nextInt();" c2="in.nextInt();" if c1 the entered order can be multiplied.. else int new multiply elements k sum="sum" multiplication result is></r1> |
Output:
Enter number of rows and columns of first matrix : 2 2
Enter first matrix elements : 2 4 6 8
Enter number of rows and columns of second matrix :2 2
Enter second matrix elements : 3 6 9 12
Matrix multiplication result is :
42 60
90 132
Enter first matrix elements : 2 4 6 8
Enter number of rows and columns of second matrix :2 2
Enter second matrix elements : 3 6 9 12
Matrix multiplication result is :
42 60
90 132
That’s all about mutliplying two matrices in java.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.