JAVA Program to Perform Addition of Matrix

JAVA Program to Perform Addition of Matrix:


 class Twodimadd
 {

  public static void main(String args[])
  {
       int [][] A = {{1,2,3},{4,5,6},{7,8,9}};
       int [][] B = {{1,2,3},{4,5,6},{7,8,9}};
       //int [][] B = {1,2,3,4,5,6,7,8,9};

       int i,j;
     System.out.println("Matrix A is \n");
      for( i=0;i<3;i++)
      {
      for( j=0;j<3;j++)
      {
      System.out.print(" "+A[i][j]);
      }
      System.out.println();
      }


      System.out.println("\nMatrix B is \n");
      for( i=0;i<3;i++)
      {
       for( j=0;j<3;j++)
       {
       System.out.print(" "+B[i][j]);
       }
       System.out.println();
      }

       System.out.println("\nAddition is \n");
          for( i=0;i<3;i++)
          {
           for( j=0;j<3;j++)
           {
           System.out.print("  "+(A[i][j]+B[i][j]));
           }
           System.out.println();
      }
   }
  }

OUTPUT:

Matrix A is

 1 2 3
 4 5 6
 7 8 9

Matrix B is

 1 2 3
 4 5 6
 7 8 9

Addition is

 2  4  6
 8  10 12
 14 16 18

Leave a Reply

Your email address will not be published. Required fields are marked *

thirteen + fourteen =