ADDITION OF TO MATRIX

This is the programe adding the matrix using for loop we can get the addition of matrix.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Scanner;
public class AddMatrix
{
     public static void main(String args[])
     {
          int m, n, i, j = 0;
          Scanner in = new Scanner(System.in);
          System.out.println("Enter the number of rows and columns of matrix");
          m = in.nextInt();
          n = in.nextInt();
          int first[][] = new int[m][n];
          int second[][] = new int[m][n];
          int sum[][] = new int[m][n];
          System.out.println("Enter the elements of first matrix");
          for (  i = 0 ; i < m ; i++ )
          for ( j = 0 ; j < n ; j++ )
          first[i][j] = in.nextInt();
          System.out.println("Enter the elements of second matrix");
          for ( i = 0 ; i < m ; i++ )
          for ( j = 0 ; j< n ; j++ )
          second[i][j] = in.nextInt();
          for ( i = 0 ; i < m ; i++ )
          for ( j = 0 ; j < n ; j++ )
          sum[i][j] = first[i][j]+ second[i][j];
          System.out.println("Sum of entered matrices:-");
          for ( i= 0 ; i < m ;i++ )
          {
             for ( j = 0 ; j < n ; j++ )
             System.out.print(sum[i][j]+"\t");
          }
     }
}

Output :Addition of Two Matrix

addm