Sunday, 17 July 2011

Write a program of addition of matrix

import java.io.*;
class Matrix1
{
int row,i,j;
int col;
int n[][]=new int[10][10];

public void input()
throws IOException
{
          BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
          System.out.println("enter the order of matrix");
          row=Integer.parseInt(br.readLine());
          col=Integer.parseInt(br.readLine());
          System.out.println("enter the matrix");
          for(i=0;i<row;i++)
          {
                   for(j=0;j<col;j++)
                   {   BufferedReader cr=new BufferedReader(new InputStreamReader(System.in));
                             n[i][j]=Integer.parseInt(cr.readLine());
                   }
          }
}
          public void output()
          {
                   System.out.println("matrix is");
                   for(i=0;i<row;i++)
                             {
                                      for(j=0;j<col;j++)
                                      {

                                                System.out.print(n[i][j]+"\t");
                                      }
                                      System.out.print("\n");
          }
}
 public void sum(Matrix1 m1,Matrix1 m2)
 {
           Matrix1 t3=new Matrix1();
           row=m1.row;
           col=m1.col;
           for(i=0;i<row;i++)
                    {
                             for(j=0;j<col;j++)
                             {
                                       t3.n[i][j]=m1.n[i][j]+m2.n[i][j];
                             }
                   }

                   t3.output();
          }
}


class matmain
{
          public static void main(String[] arg)
          throws IOException
          {
                   Matrix1 m1=new Matrix1();
                   Matrix1 m2=new Matrix1();
                   Matrix1 m3=new Matrix1();
                   m1.input();
                   m2.input();
                   m1.output();
                   m2.output();
                   m3.sum(m1,m2);

          }
}

No comments:

Post a Comment

 

About The Author


I am a B.Tech Computer Science Student. I love Programming and blogging. I am Oracle certified Java Professional. Read More
Indian Blogger

Teachers Jobs

Footer 3

Twitter Facebook RSS YouTube Google
​

All Rights Reserved ©Sun Java Masters 2013