Sunday, August 21, 2011

Sum of two matrix in java


//Sum of two matrix in java
import java.io.*;
class arraysum
{
 public static void main(String[] arg)
  {
   int m,n;
   BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
   int a[ ][ ]=new int[30][30];
   int b[ ][ ]=new int[30][30];
   int c[ ][ ]=new int[30][30];
    try
    {
     System.out.print("Enter the number of  rows: ");
      m=Integer.parseInt(in.readLine());
     System.out.print("Enter the number of column: ");
      n=Integer.parseInt(in.readLine());  System.out.print("Enter the value of elements of first array: ");    
      for(int i=0;i< m;i++)
      {
       for(int j=0;j<n;j++)
       {      
        a[i][j]=Integer.parseInt(in.readLine());
        }
      }
      System.out.print("Enter the value of elements of second array: ");    
      for(int i=0;i< m;i++)
      {
       for(int j=0;j<n;j++)
       {      
        b[i][j]=Integer.parseInt(in.readLine());
        }
      }
      System.out.println("First matrix:");
       for(int i=0;i< m;i++)
      {
       for(int j=0;j<n;j++)
       {      
        System.out.print(a[i][j]+" ");
        }
        System.out.println();
      }
     System.out.println("Second matrix:");
       for(int i=0;i< m;i++)
      {
       for(int j=0;j<n;j++)
       {      
        System.out.print(b[i][j]+" ");
        }
        System.out.println();
      }

     for(int i=0;i< m;i++)
      {
       for(int j=0;j<n;j++)
       {      
        c[i][j]=a[i][j]+b[i][j];
        }
      }
      System.out.println("Sum of two matrixs:");
       for(int i=0;i< m;i++)
      {
       for(int j=0;j<n;j++)
       {      
        System.out.print(c[i][j]+" ");
        }
        System.out.println();
      }
    }
    catch(Exception e){ }
   }
}

Previous Next Home
0 Comments
Comments

Leave a Comment

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Twitter Delicious Facebook Favorites More