java

Write a Java Program to Calculate the Multiplication of Two Matrices

In this tutorial, we are going to see how to write a Java program to calculate the multiplication of two matrices. Before we jump to the code, let’s see how it’s look like to calculate the multiplication of two matrices.
 


 
 

Java Program to Calculate the Multiplication of Two Matrices:
public class Main{
	public static void main(String args[]){
		//Create two matrices
		int A[][] = {{1,2},{3,4}};
		int B[][] = {{5,6},{7,8}};

		//Create a new matrix to store the result of multiplication
		//2 rows and 2 columns	
		int C[][] = new int[2][2];  

		//multiplication
		for(int i=0; i<2; i++){
			for(int j=0; j<2; j++){ 
				C[i][j] = 0;    
				for(int k=0; k<2 ;k++)    
				{ 
					C[i][j] += A[i][k] * B[k][j];    
				}
				System.out.print(C[i][j]+" "); 
			}
			System.out.println();
		}  
	}
}

Output:

19 22 
43 50
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *