Sunday, August 12, 2012

Java Program to perform Matrix addition


class matrixaddition
{
int a[][]={{1,2,5},{1,0,2},{0,0,1}};
int b[][]={{2,2,4},{6,6,4},{3,7,8}};
int sum[][];
matrixaddition()
{
sum=new int[3][3];
}
void display1()
{
for(int i=0;i<3;i++)
  {
   for(int j=0;j<3;j++)
        System.out.print(a[i][j]+" ");
     System.out.println("\t");
  }
}
void display2()
{
for(int i=0;i<3;i++)
  {
   for(int j=0;j<3;j++)
        System.out.print(b[i][j]+" ");
     System.out.println("\t");
  }
}
void addmatrix()
{
for(int i=0;i<3;i++)
  {
   for(int j=0;j<3;j++)
       sum[i][j]=a[i][j]+b[i][j];
  }
}
void summatrix()
{
for(int i=0;i<3;i++)
  {
   for(int j=0;j<3;j++)
        System.out.print(sum[i][j]+" ");
     System.out.println("\t");
  }
}
public static void main(String s[])
{
matrixaddition ma=new matrixaddition();
ma.display1();
System.out.println("\n");
ma.display2();
System.out.println("\n");
ma.addmatrix();
System.out.println("\n");
ma.summatrix();
}
}

0 comments:

Post a Comment