How to Add Two Matrices in Java Program

Learn how to add two matrices in Java with a simple example. Read matrix values using Scanner and perform matrix addition.

Code Explanation (Step-by-Step)

  • Creates a Scanner object to read input values from the user.
  • Reads the number of rows and columns for the matrices.
  • Creates two matrices to store the input values.
  • Traverses each row and column of the first and second matrix to stores the entered values.
  • Creates a new matrix to store the result of the addition .
  • Traverses the corresponding elements of both matrices and adds them.
  • Stores the sum in the result matrix c[i][j] at the corresponding position.
  • Traverses the result matrix row by row and column by column.
  • Prints the elements of the resultant matrix.
				
					import java.util.Scanner;

public class MatrixAddition {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		System.out.print("Enter number of rows:- ");
		int rows = sc.nextInt();
		System.out.print("Enter number of columns:- ");
		int columns = sc.nextInt();

		int[][] a = new int[rows][columns];
		int[][] b = new int[rows][columns];

		System.out.println("Enter the first matrix value:- ");
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < columns; j++) {
				a[i][j] = sc.nextInt();
			}
		}
		System.out.println("Enter the second matrix value :- ");
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < columns; j++) {
				b[i][j] = sc.nextInt();
			}
		}
		int[][] c = new int[rows][columns];
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < columns; j++) {
				c[i][j] = a[i][j] + b[i][j];
			}
		}
		System.out.println("The sum of the two matrices is:- ");
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < columns; j++) {
				System.out.print(c[i][j] + " ");
			}
			System.out.println();
		}
	}
}
				
			

Output :-
Enter number of rows:- 2
Enter number of columns:- 2
Enter the first matrix value:-
3
4
5
6
Enter the second matrix value :-
7
8
9
2
The sum of the two matrices is:-
10 12
14 8

Java Program to Add Two Matrices

				
					import java.util.Scanner;

public class MatrixAdditionMethod {

    public static void readMatrix(int[][] matrix, Scanner sc) {
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                matrix[i][j] = sc.nextInt();
            }
        }
    }

    public static int[][] addMatrices(int[][] a, int[][] b) {
        int[][] result = new int[a.length][a[0].length];

        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                result[i][j] = a[i][j] + b[i][j];
            }
        }
        return result;
    }

    public static void printMatrix(int[][] matrix) {
        for (int[] row : matrix) {
            for (int value : row) {
                System.out.print(value + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter number of rows: ");
        int rows = sc.nextInt();

        System.out.print("Enter number of columns: ");
        int cols = sc.nextInt();

        int[][] matrix1 = new int[rows][cols];
        int[][] matrix2 = new int[rows][cols];

        System.out.println("Enter first matrix values:");
        readMatrix(matrix1, sc);

        System.out.println("Enter second matrix values:");
        readMatrix(matrix2, sc);

        int[][] sum = addMatrices(matrix1, matrix2);

        System.out.println("Sum of matrices:");
        printMatrix(sum);

        sc.close();
    }
}
				
			

FAQ
What is matrix addition in Java?
Matrix addition is the process of adding corresponding elements of two matrices having the same number of rows and columns.
Can two matrices of different sizes be added?
No, both matrices must have the same dimensions for matrix addition.
Which data structure is used for matrix addition in Java?
A two-dimensional array (2D array) is commonly used to store and add matrices.
What is the time complexity of matrix addition?
The time complexity is O(rows × columns) because every element is visited once.