You are here: Home / Topics / Jav Program to find sum of two matrices

Jav Program to find sum of two matrices

Filed under: Java on 2023-08-23 06:53:38

// Program to addition of two Matrix

class  MatrixAddition
{
public static void main( String args[ ] )
{
 int  i, j;
 int  c[ ][ ] = new int[3][3];
 
 int  a[ ][ ] = {  
    {1, 2, 3},
     {4, 5, 6},
     {7, 8, 9}  };
 int  b[ ][ ] = {  
    {9, 8, 7},
     {6, 5, 4},
     {3, 2, 1}  };

 for( i=0 ; i<3 ; i++ )
 {
  for( j=0 ; j<3 ; j++ )
  {
   c[ i ][ j ] = a[ i ][ j ] + b[ i ][ j ];
  }
 }

 System.out.println( " The sum of matrices is : " );
 for( i=0 ; i<3 ; i++ )
 {
  for( j=0 ; j<3 ; j++ )
  {
   System.out.print( "    " + c[ i ][ j ] );
  }
  System.out.println();
 }
}
}


Output:

The sum of matrices is : 
10  10  10
10  10  10
10  10  10

About Author:
J
Java     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.