21 December 2009

sum of matrix in java

In this program we are going to calculate the sum of two matrix. To make this program, we need to declare two dimensional array of type integer. Firstly it calculates the length of the both the arrays. Now we need to make a matrix out of it. To make the matrix we will use the for loop. By making use of the for loop the rows and column will get divide. This process will be performed again for creating the second matrix.
After getting both the matrix with us, we need to sum both the matrix. The both matrix will be added by using the for loop with array[i][j]+array1[i][j]. The output will be displayed by using the println() method.


class summatrix
{
    public static void main(String[] args) 
            {
            int array[][]= {{4,5,6},{6,8,9}};
            int array1[][]= {{5,4,6},{5,6,7}};
            System.out.println("Number of Row= " + array.length);
            System.out.println("Number of Column= " + array[1].length);
            int l= array.length;
            System.out.println("Matrix 1 : ");
           
            for(int i = 0; i < l; i++)
                {
                for(int j = 0; j <= l; j++)
                    {
                    System.out.print(" "+ array[i][j]);
                    } 
                System.out.println();
                } 
            int m= array1.length;
            System.out.println("Matrix 2 : ");
            for(int i = 0; i < m; i++)
                {
                  for(int j = 0; j <= m; j++)
                      {
                        System.out.print(" "+array1[i][j]);
                       } 
                  System.out.println();
                }
            System.out.println("Addition of both matrix : ");
            for(int i = 0; i < m; i++)
                {
                  for(int j = 0; j <= m; j++)
                      {
                        System.out.print(" "+(array[i][j]+array1[i][j]));
                      } 
                System.out.println();
                }
            }
}



1 comment:

  1. I love your sharing,i learn a lot from your post.There is a lot of very useful knowledge in your post.I enjoy reading it and hope to see more.Can you write more about this topic?I am very interested in it.Waiting for your new post.
    encode java barcode

    ReplyDelete