Java Program to Add Two Matrices with Output
for loops to add corresponding elements, storing the result in a new matrix, like result[i][j] = matrix1[i][j] + matrix2[i][j];.Examples
How to Think About It
Algorithm
Code
public class MatrixAddition { public static void main(String[] args) { int[][] matrix1 = {{1, 2}, {3, 4}}; int[][] matrix2 = {{5, 6}, {7, 8}}; int rows = matrix1.length; int cols = matrix1[0].length; int[][] result = new int[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result[i][j] = matrix1[i][j] + matrix2[i][j]; } } System.out.println("Sum of matrices:"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { System.out.print(result[i][j] + " "); } System.out.println(); } } }
Dry Run
Let's trace adding matrix1 = {{1, 2}, {3, 4}} and matrix2 = {{5, 6}, {7, 8}} through the code.
Initialize matrices and result
matrix1 = {{1, 2}, {3, 4}}, matrix2 = {{5, 6}, {7, 8}}, result = {{0, 0}, {0, 0}}
Add element at position (0,0)
result[0][0] = 1 + 5 = 6
Add element at position (0,1)
result[0][1] = 2 + 6 = 8
Add element at position (1,0)
result[1][0] = 3 + 7 = 10
Add element at position (1,1)
result[1][1] = 4 + 8 = 12
Final result matrix
result = {{6, 8}, {10, 12}}
| Position | matrix1[i][j] | matrix2[i][j] | result[i][j] |
|---|---|---|---|
| (0,0) | 1 | 5 | 6 |
| (0,1) | 2 | 6 | 8 |
| (1,0) | 3 | 7 | 10 |
| (1,1) | 4 | 8 | 12 |
Why This Works
Step 1: Matrix elements correspond by position
Each element in the first matrix is added to the element in the same row and column of the second matrix using result[i][j] = matrix1[i][j] + matrix2[i][j];.
Step 2: Use nested loops to cover all elements
Two loops go through rows and columns to access every element systematically.
Step 3: Store sums in a new matrix
The sums are saved in a new matrix so the original matrices stay unchanged.
Alternative Approaches
import java.util.Scanner; public class MatrixAdditionInput { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int rows = 2, cols = 2; int[][] matrix1 = new int[rows][cols]; int[][] matrix2 = new int[rows][cols]; System.out.println("Enter elements of first matrix:"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { matrix1[i][j] = sc.nextInt(); } } System.out.println("Enter elements of second matrix:"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { matrix2[i][j] = sc.nextInt(); } } int[][] result = new int[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result[i][j] = matrix1[i][j] + matrix2[i][j]; } } System.out.println("Sum of matrices:"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { System.out.print(result[i][j] + " "); } System.out.println(); } sc.close(); } }
public class MatrixAdditionCheck { public static void main(String[] args) { int[][] matrix1 = {{1, 2, 3}, {4, 5, 6}}; int[][] matrix2 = {{7, 8, 9}, {10, 11, 12}}; if (matrix1.length != matrix2.length || matrix1[0].length != matrix2[0].length) { System.out.println("Matrices must be of same size to add."); return; } int rows = matrix1.length; int cols = matrix1[0].length; int[][] result = new int[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result[i][j] = matrix1[i][j] + matrix2[i][j]; } } System.out.println("Sum of matrices:"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { System.out.print(result[i][j] + " "); } System.out.println(); } } }
Complexity: O(rows * cols) time, O(rows * cols) space
Time Complexity
The program uses nested loops to visit each element once, so the time grows with the number of elements, which is rows times columns.
Space Complexity
A new matrix is created to store the sum, so space used is proportional to the size of the matrices.
Which Approach is Fastest?
All approaches have similar time complexity; using user input adds overhead but does not change complexity.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Fixed matrices in code | O(rows * cols) | O(rows * cols) | Simple, quick testing |
| User input matrices | O(rows * cols) | O(rows * cols) | Dynamic input from user |
| With size validation | O(rows * cols) | O(rows * cols) | Safe addition with error checking |