Java Program to Multiply Two Matrices

«
»
This is a java program to perform a simple matrix multiplication. For matrix multiplication to happen the column of the first matrix should be equal to the row of the second matrix.

Here is the source code of the Java Program to Perform Matrix Multiplication. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. // This is sample program for matrix multiplication
  2. // The complexity of the algorithm is O(n^3)
  3. package com.sanfoundry.numerical;
  4.  
  5. import java.util.Scanner;
  6.  
  7. public class MatixMultiplication
  8. {
  9.     public static void main(String args[])
  10.     {
  11.         int n;
  12.         Scanner input = new Scanner(System.in);
  13.         System.out.println("Enter the base of squared matrices");
  14.         n = input.nextInt();
  15.         int[][] a = new int[n][n];
  16.         int[][] b = new int[n][n];
  17.         int[][] c = new int[n][n];
  18.         System.out.println("Enter the elements of 1st martix row wise \n");
  19.         for (int i = 0; i < n; i++)
  20.         {
  21.             for (int j = 0; j < n; j++)
  22.             {
  23.                 a[i][j] = input.nextInt();
  24.             }
  25.         }
  26.         System.out.println("Enter the elements of 2nd martix row wise \n");
  27.         for (int i = 0; i < n; i++)
  28.         {
  29.             for (int j = 0; j < n; j++)
  30.             {
  31.                 b[i][j] = input.nextInt();
  32.             }
  33.         }
  34.         System.out.println("Multiplying the matrices...");
  35.         for (int i = 0; i < n; i++)
  36.         {
  37.             for (int j = 0; j < n; j++)
  38.             {
  39.                 for (int k = 0; k < n; k++)
  40.                 {
  41.                     c[i][j] = c[i][j] + a[i][k] * b[k][j];
  42.                 }
  43.             }
  44.         }
  45.         System.out.println("The product is:");
  46.         for (int i = 0; i < n; i++)
  47.         {
  48.             for (int j = 0; j < n; j++)
  49.             {
  50.                 System.out.print(c[i][j] + " ");
  51.             }
  52.             System.out.println();
  53.         }
  54.         input.close();
  55.     }
  56. }

Output:

 
Output:
$ javac MatixMultiplication.java
$ java MatixMultiplication
 
Enter the base of squared matrices:
3
Enter the elements of 1st martix row wise:
1 2 3
4 5 6
7 8 9
Enter the elements of 2nd martix row wise:
2 3 4
5 6 7
8 9 1
Multiplying the matrices...
The product is:
36 42 21
81 96 57
126 150 93

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.