Java Program to Add Two Matrices

«
»
This is a Java Program to Add Two MXN Matrix from User Input.

Enter the elements of two arrays as input. Each element of the first matrix will added to the corresponding element in the second matrix and get the resultant matrix.

Here is the source code of the Java Program to Add Two MXN Matrix from User Input. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2. public class Add_Matrix
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.         int p, q, m, n;
  7.         Scanner s = new Scanner(System.in);
  8.         System.out.print("Enter number of rows in first matrix:");
  9.         p = s.nextInt();
  10.         System.out.print("Enter number of columns in first matrix:");
  11.         q = s.nextInt();
  12.         System.out.print("Enter number of rows in second matrix:");
  13.         m = s.nextInt();
  14.         System.out.print("Enter number of columns in second matrix:");
  15.         n = s.nextInt();
  16.         if (p == m && q == n) 
  17.         {
  18.             int a[][] = new int[p][q];
  19.             int b[][] = new int[m][n];
  20.             int c[][] = new int[m][n];
  21.             System.out.println("Enter all the elements of first matrix:");
  22.             for (int i = 0; i < p; i++) 
  23.             {
  24.                 for (int j = 0; j < q; j++) 
  25.                 {
  26.                     a[i][j] = s.nextInt();
  27.                 }
  28.             }
  29.             System.out.println("Enter all the elements of second matrix:");
  30.             for (int i = 0; i < m; i++) 
  31.             {
  32.                 for (int j = 0; j < n; j++) 
  33.                 {
  34.                     b[i][j] = s.nextInt();
  35.                 }
  36.             }
  37.             System.out.println("First Matrix:");
  38.             for (int i = 0; i < p; i++) 
  39.             {
  40.                 for (int j = 0; j < q; j++) 
  41.                 {
  42.                     System.out.print(a[i][j]+" ");
  43.                 }
  44.                 System.out.println("");
  45.             }
  46.             System.out.println("Second Matrix:");
  47.             for (int i = 0; i < m; i++) 
  48.             {
  49.                 for (int j = 0; j < n; j++) 
  50.                 {
  51.                     System.out.print(b[i][j]+" ");
  52.                 }
  53.                 System.out.println("");
  54.             }
  55.             for (int i = 0; i < p; i++) 
  56.             {
  57.                 for (int j = 0; j < n; j++) 
  58.                 {
  59.                     for (int k = 0; k < q; k++) 
  60.                     {
  61.                         c[i][j] = a[i][j] + b[i][j];
  62.                     }
  63.                 }
  64.             }
  65.             System.out.println("Matrix after addition:");
  66.             for (int i = 0; i < p; i++) 
  67.             {
  68.                 for (int j = 0; j < n; j++) 
  69.                 {
  70.                     System.out.print(c[i][j]+" ");
  71.                 }
  72.                 System.out.println("");
  73.             }
  74.         }
  75.         else
  76.         {
  77.             System.out.println("Addition would not be possible");
  78.         }
  79.     }
  80. }

Output:

$ javac Add_Matrix.java
$ java Add_Matrix
 
Enter number of rows in first matrix:2
Enter number of columns in first matrix:3
Enter number of rows in second matrix:2
Enter number of columns in second matrix:3
Enter all the elements of first matrix:
1
2
3
4
5
6
Enter all the elements of second matrix:
7
8
9
4
3
2
First Matrix:
1 2 3 
4 5 6 
Second Matrix:
7 8 9 
4 3 2 
Matrix after addition:
8 10 12 
8 8 8

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.