Addition of matrix


CODE:
import java.util.*;
class matrixadd
{
     public static void main(String args[])
     {
           int m,n,i,j;
           Scanner sc = new Scanner(System.in);
           System.out.print("Enter no of rows & columns :");
           m=sc.nextInt();
           n=sc.nextInt();
           int a[][]=new int[m][n];
           int b[][]=new int[m][n];
           int c[][]=new int[m][n];
           System.out.print("Matrix A:");
           for(i=0;i<=m-1;i++)
           {
                for(j=0;j<=n-1;j++)
                {
                     System.out.print("enter a no:");
                     a[i][j]=sc.nextInt();
                }
           }
           System.out.print("Matrix B:");
           for(i=0;i<=m-1;i++)
           {
                for(j=0;j<=n-1;j++)
                {
                     System.out.print("enter a no:");
                     b[i][j]=sc.nextInt();
                }
           }
           for(i=0;i<=m-1;i++)
           {
                for(j=0;j<=n-1;j++)
                {
                     c[i][j]=a[i][j]+b[i][j];
                }
           }
           System.out.println("Sum Matrix");
           for(i=0;i<=m-1;i++)
           {
                for(j=0;j<=n-1;j++)
                {
                     System.out.print(c[i][j]+"\t");
                }
                System.out.println();
           }
     }
}         
          
OUTPUT:


No comments :

Post a Comment