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}\)
Write a C program which calculates the addition of two matrices of the same size.
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.
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.
/*
* Matrix Addition program in Java
*/
import java.util.Scanner;
public class Add_Matrix
{
public static void main(String[] args)
{
int p, q, m, n;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of rows in first matrix:");
p = s.nextInt();
System.out.print("Enter number of columns in first matrix:");
q = s.nextInt();
System.out.print("Enter number of rows in second matrix:");
m = s.nextInt();
System.out.print("Enter number of columns in second matrix:");
n = s.nextInt();
if (p == m && q == n)
{
int a[][] = new int[p][q];
int b[][] = new int[m][n];
int c[][] = new int[m][n];
System.out.println("Enter all the elements of first matrix:");
for (int i = 0; i < p; i++)
{
for (int j = 0; j < q; j++)
{
a[i][j] = s.nextInt();
}
}
System.out.println("Enter all the elements of second matrix:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
b[i][j] = s.nextInt();
}
}
System.out.println("First Matrix:");
for (int i = 0; i < p; i++)
{
for (int j = 0; j < q; j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println("");
}
System.out.println("Second Matrix:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println("");
}
for (int i = 0; i < p; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < q; k++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
}
System.out.println("Matrix after addition:");
for (int i = 0; i < p; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println("");
}
}
else
{
System.out.println("Addition would not be possible");
}
}
}
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.
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 Matrix after addition: 8 10 12 8 8 8
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
- Practice Programming MCQs
- Practice Information Technology MCQs
- Apply for Computer Science Internship
- Check Programming Books
- Apply for Java Internship