Java Program to add two matrices

Program

import java.util.Scanner;
class AddMatrix
{
    public static void main(String[] args) {
        int i, j;
        int row, col;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of rows for both matrix:\t");
        row = sc.nextInt();
        System.out.println("Enter the number of columns for both matrix:\t");
        col = sc.nextInt();
        int mat1[][] = new int[row][col];
        int mat2[][] = new int[row][col];
        int mat3[][] = new int[row][col];
        System.out.printf("Enter %d elements for matrix 1\n", row * col);
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                mat1[i][j] = sc.nextInt();
            }
        }
        System.out.printf("Enter %d elements for matrix 2\n", row * col);
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                mat2[i][j] = sc.nextInt();
            }
        }
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                mat3[i][j] = mat1[i][j] + mat2[i][j];
            }
        }
        System.out.println("The Addition of two Matrices is:\n");
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                System.out.printf("%d\t", mat3[i][j]);
            }
            System.out.println("\n");
        }
    }
}

This program performs matrix addition in Java.

  1. Input:

    • The program first asks the user for the number of rows and columns for two matrices (mat1 and mat2).
    • It then prompts the user to enter the elements for both matrices.
  2. Matrix Addition Logic:

    • The elements of the two matrices are added element by element. The sum of corresponding elements from mat1 and mat2 is stored in a third matrix (mat3).
  3. Steps:

    • Matrix Input:
      • Two matrices mat1 and mat2 are created based on the given dimensions (rows and columns).
      • User input is used to fill the elements of these matrices.
    • Matrix Addition:
      • A nested loop iterates over all elements of both matrices, adding the corresponding elements (mat1[i][j] + mat2[i][j]) and storing the result in mat3.
    • Matrix Output:
      • Finally, the result matrix mat3 (sum of the two matrices) is printed row by row.
  4. Output:

    • The result matrix is displayed after performing the addition of corresponding elements from mat1 and mat2.
  5. Example Execution:

    • Input (for example, a 2x2 matrix):
      Enter the number of rows for both matrix: 2
      Enter the number of columns for both matrix: 2
      Enter 4 elements for matrix 1:
      1 2
      3 4
      Enter 4 elements for matrix 2:
      5 6
      7 8
      
    • Process:
      • Matrix 1:
        1 2
        3 4
        
      • Matrix 2:
        5 6
        7 8
        
      • The sum will be:
        6 8
        10 12
        
    • Output:
      The Addition of two Matrices is:
      6   8   
      10  12  
      
  6. Explanation:

    • The program adds matrices element by element:
      • For each element mat3[i][j], it adds mat1[i][j] and mat2[i][j].
    • This program works for any matrix size, as long as both matrices have the same dimensions.

Time Complexity:

  • Time Complexity: O(m * n), where m is the number of rows and n is the number of columns. This is because we loop over every element of the matrices to perform the addition.

Space Complexity:

  • Space Complexity: O(m * n) for storing the matrices and their sum.

Output

$ javac AddMatrix.java
$ java AddMatrix
Enter the number of rows for both matrix:
3
Enter the number of columns for both matrix:
3
Enter 9 elements for matrix 1
12
56
77
98
14
66
15
38
12
Enter 9 elements for matrix 2
78
96
54
23
85
25
47
66
58
The Addition of two Matrices is:
90      152     131
121     99      91
62      104     70