Java Program to add two matrix


January 8, 2023, Learn eTutorial
1406

In this Java program, we are discussing adding two matrices. For that, we have to read the elements of 2 matrices A, and B, then find out the sum of A and B into the resultant matrix sum.

How to declare a matrix in java?

A multi-dimensional array is called a matrix. The syntax for declaring a two-dimensional array is shown below.

Data_type[1st dimension][2nd Dimension]array_name=new Data_type[Size1][Size2];

An example is shown below:

int[][] A=new[10][10];

Here the matrix A is created with a size of 10 X 10.

How to implement matrix addition in a Java program?

First, we have to declare the class AddMatrix.Then open the main function. Create an object of the scanner class as in. Read the number of rows and columns into the variables m and n. Then Initialize three matrices of size m and n. Then by using a for loop read the elements of the matrix A.Then read the elements of the matrix B.After that calculate the sum matrix as sum[i][j]=A[i][j]+B[i][j]  using nested for loop.Then display each element of matrix sum .as the sum matrix.

ALGORITHM

STEP 1: Declare the class AddMatrix with a public modifier.

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3: Declare the integer variable m,n,i,j.

STEP 4: Read the number of rows and columns of the matrix into the variables m and n.

STEP 5: Initialize the matrix A[][],B[][] and sum[][] as integer.

STEP 6: Read the elements of the A into the A[i][j] using for loop.

STEP 7: Read the elements of the into the B[i][j] using for loop.

STEP 8: By using a nested for loop calculate the sum as sum[i][j]=A[i][j]+B[i][j].

STEP 9: Display the sum as sum[i][j] using nested for loop.

 

Java Source Code

                                          import java.util.Scanner;
public class AddMatrix {
 public static void main(String args[])
   {
      int m, n, i, j;
      Scanner in = new Scanner(System.in);
 
      System.out.println("Input number of rows of matrix: ");
      m = in.nextInt();
      System.out.println("Input number of columns of matrix: ");
      n  = in.nextInt();
 
      int A[][] = new int[m][n];
      int B[][] = new int[m][n];
      int sum[][] = new int[m][n];
 
      System.out.println("Input elements of first matrix: ");
 
      for (  i = 0 ; i < m ; i++ )
         for ( j = 0 ; j < n ; j++ )
            A[i][j] = in.nextInt();
 
      System.out.println("Input the elements of second matrix: ");
 
      for ( i = 0 ; i < m ; i++ )
         for ( j = 0 ; j < n ; j++ )
            B[i][j] = in.nextInt();
 
      for ( i = 0 ; i < m ; i++ )
         for ( j = 0 ; j < n ; j++ )
             sum[i][j] = A[i][j] + B[i][j]; 
 
      System.out.println("Sum of the matrices:-");
 
      for ( i = 0 ; i < m ; i++ )
      {
         for ( j = 0 ; j < n ; j++ )
            System.out.print(sum[i][j]+"\t");
 
         System.out.println();
      }
   }
}
                                      

OUTPUT

Input number of rows of matrix: 2
Input number of columns of matrix: 2
Input elements of first matrix: 
1
2
3
4
Input the elements of second matrix: 
5
6
7
8
Sum of the matrices:-
6 8 
10 12