Matrix operations

import java.io.*;
class Matrix{
 public static void main(String ar[])throws IOException{
  int a[][]={{1,2,3},{2,2,2},{1,2,3}};
  int b[][]={{1,2,3},{2,2,2},{1,2,3}};
  int c[][]=new int[3][3];
  /*
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  System.out.println("First Array");
  for(int i=0;i<3;i++){
   for(int j=0;j<3;j++){
    System.out.println("Enter element of "+i+","+j);
    a[i][j]=Integer.parseInt(br.readLine());
   }
  }
  
  System.out.println("Second Array");
  for(int i=0;i<3;i++){
   for(int j=0;j<3;j++){
    System.out.println("Enter element of "+i+","+j);
    b[i][j]=Integer.parseInt(br.readLine());
   }
  }
  */
  //adding
  for(int i=0;i<3;i++){
   for(int j=0;j<3;j++){  
    c[i][j]=a[i][j]+b[i][j];
   }
  }
  
  System.out.println("First Array");
  for(int i=0;i<3;i++){
   for(int j=0;j<3;j++){
    System.out.print(a[i][j]+" "); 
   }
   System.out.println("");
  }
  System.out.println("Second Array");
  for(int i=0;i<3;i++){
   for(int j=0;j<3;j++){
    System.out.print(b[i][j]+" "); 
   }
   System.out.println("");
  }
  System.out.println("Addition of Arrays");
  for(int i=0;i<3;i++){
   for(int j=0;j<3;j++){
    System.out.print(c[i][j]+" "); 
   }
   System.out.println("");
  }
 }
}

No comments:

Popular Posts