Walk Lightly on this PLANET and yet leave such a FOOTPRINT that cannot be erased for thousands of Years..!!!
Visit Codstech for Cyber Security related Posts !

Visitors

Wednesday, August 29, 2012

CJ-20 : Matrix addition

Type the programs in NOTEPAD or DOS prompt itself, for Consol Application.
Type in NETBEANS or ECLIPSE for Window Application.
Here after , I am just typing the programs only..to know more about it , refer

Introduction to JAVA 

before doing any JAVA Programs.
***********************************************************************************************
import java.io.*;
public class matrixsum 
{
    public static void main(String args[])throws Exception
    {
        DataInputStream din=new DataInputStream(System.in);
        int twod[][]=new int[10][10];
        int twod1[][]=new int[10][10];
        int sum[][]=new int[10][10];
        int i,j,r,c;
        System.out.println("Enter the number of rows and column of the matrix");
        r=Integer.parseInt(din.readLine());
        c=Integer.parseInt(din.readLine());
        System.out.println("Enter the number of first matrix");
        for(i=0;i<r;i++)
        {
            for(j=0;j<c;j++)
            {
                twod[i][j]=Integer.parseInt(din.readLine());
            }
        }
        System.out.println("The first matrix are :");
        for(i=0;i<r;i++)
        {
            for(j=0;j<c;j++)
            {
                System.out.print(twod[i][j]+" ");
            }
            System.out.println(); 
        }
        System.out.println("Enter the number of second matrix");
        for(i=0;i<r;i++)
        {
            for(j=0;j<c;j++)
            {
                twod1[i][j]=Integer.parseInt(din.readLine());
            }
        }
        System.out.println("The second matrix are :");
        for(i=0;i<r;i++)
        {
            for(j=0;j<c;j++)
            {
                System.out.print(twod1[i][j]+" ");
            }
            System.out.println(); 
        }
        System.out.println("The sum of the matrix are :");
        for(i=0;i<r;i++)
        {
            for(j=0;j<c;j++)
            {
                sum[i][j]=twod[i][j]+twod1[i][j];
                System.out.print(sum[i][j]+" ");
            }
            System.out.println();
        }
    }
}
=========================================================================
Output : 

Enter the number of rows and column of the matrix
2
2
Enter the number of first matrix
2
3
4
5
The first matrix are :
2 3 
4 5 
Enter the number of second matrix
5
4
1
2
The second matrix are :
5 4 
1 2 
The sum of the matrix are :
7 7 
5 7 

No comments: