Given two matrices A and B of same size, the task to add them in Java.
Examples:
Input: A[][] = {{1, 2},
{3, 4}}
B[][] = {{1, 1},
{1, 1}}
Output: {{2, 3},
{4, 5}}
Input: A[][] = {{2, 4},
{3, 4}}
B[][] = {{1, 2},
{1, 3}}
Output: {{3, 6},
{4, 7}}
Approach:
- Take the two matrices to be added
- Create a new Matrix to store the sum of the two matrices
- Traverse each element of the two matrices and add them. Store this sum in the new matrix at the corresponding index.
- Print the final new matrix
Below is the implementation of the above approach:
// Java program for addition of two matrices import java.io.*; class GFG { // Function to print Matrix static void printMatrix(int M[][], int rowSize, int colSize) { for (int i = 0; i < rowSize; i++) { for (int j = 0; j < colSize; j++) System.out.print(M[i][j] + " "); System.out.println(); } } // Function to add the two matrices // and store in matrix C static int[][] add(int A[][], int B[][], int size) { int i, j; int C[][] = new int[size][size]; for (i = 0; i < size; i++) for (j = 0; j < size; j++) C[i][j] = A[i][j] + B[i][j]; return C; } // Driver code public static void main(String[] args) { int size = 4; int A[][] = { { 1, 1, 1, 1 }, { 2, 2, 2, 2 }, { 3, 3, 3, 3 }, { 4, 4, 4, 4 } }; // Print the matrices A System.out.println("\nMatrix A:"); printMatrix(A, size, size); int B[][] = { { 1, 1, 1, 1 }, { 2, 2, 2, 2 }, { 3, 3, 3, 3 }, { 4, 4, 4, 4 } }; // Print the matrices B System.out.println("\nMatrix B:"); printMatrix(B, size, size); // Add the two matrices int C[][] = add(A, B, size); // Print the result System.out.println("\nResultant Matrix:"); printMatrix(C, size, size); } } |
chevron_right
filter_none
Output:
Matrix A: 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 Matrix B: 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 Resultant Matrix: 2 2 2 2 4 4 4 4 6 6 6 6 8 8 8 8
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Python program to add two Matrices
- Java Program to Multiply two Matrices of any size
- Program to multiply two matrices
- Program for addition of two matrices
- Program to check if two given matrices are identical
- Python program to multiply two matrices
- Python List Equality | Program to check if two given matrices are identical
- Program to concatenate two given Matrices of same size
- Multiplication of two Matrices in Single line using Numpy in Python
- Kronecker Product of two matrices
- Count pairs from two sorted matrices with given sum
- Find the intersection of two Matrices
- Minimum elements to be added so that two matrices can be multiplied
- Check if the given two matrices are mirror images of one another
- Program for subtraction of matrices
- Count sub-matrices having sum divisible 'k'
- A square matrix as sum of symmetric and skew-symmetric matrices
- Different Operations on Matrices
- Queries on number of Binary sub-matrices of Given size
- Count of matrices (of different orders) with given number of elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

