# Java Program to Add Two Matrices

Matrix addition in Java is used to add two matrices. i.e. calculate and print the sum of them.

Example:

Given two matrices of the same size, this program will add the corresponding elements of each matrix and print the result.

Input:
First Matrix: $$\begin{bmatrix} 1 & 2 & 3\\ 4 & 5 & 6 \end{bmatrix}$$

Second Matrix: $$\begin{bmatrix} 7 & 8 & 9 \\ 4 & 3 & 2 \end{bmatrix}$$

Output:
$$\begin{bmatrix} 1 & 2 & 3\\ 4 & 5 & 6 \end{bmatrix}$$ + $$\begin{bmatrix} 7 & 8 & 9 \\ 4 & 3 & 2 \end{bmatrix}$$ = $$\begin{bmatrix} 1+7 & 2+8 & 3+9\\ 4+4 & 5+3 & 6+2 \end{bmatrix}$$ = $$\begin{bmatrix} 8 & 10 & 12 \\ 8 & 8 & 8 \end{bmatrix}$$

Problem Description

Write a C program which calculates the addition of two matrices of the same size.

Problem Solution

In order to add two matrices, we need to add the corresponding elements of each matrix. Suppose we have two matrices of size m x n and p x q.

Algorithm:
1. Take the number of rows and columns as input.
2. Store the number in a variable.
3. Initialize the first matrix.
4. Initialize the second matrix.
5. Initialize the result matrix.
6. Inside a loop, add the corresponding elements of the two matrices and store them in the result matrix.
7. Print the result matrix.

Program/Source Code
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

1. The program uses the Scanner class to get user input for the number of rows and columns for two matrices.
2. The program checks if the dimensions of both matrices are the same. If not, it displays an error message.
3. If the dimensions are the same, the program creates three matrices, named a, b, and c, of the same dimensions.
4. The user is then prompted to enter the elements of matrices a and b.
5. It prints the input matrices a and b.
6. It adds the two matrices element-wise, storing the result in c.
7. Finally, it prints the resulting matrix c.

Runtime Test Cases

In this case, we enter “2” for the number of rows and “3” for the number of columns as input for the addition of two matrices in Java.

$javac Matrix_Addition.java$ java Matrix_Addition

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
8 10 12
8 8 8

Sanfoundry Global Education & Learning Series – 1000 Java Programs.