Program to add two matrix of size m*n

Program:
import java.util.*;
class Mat
{
public static void main(String args[])
{
int i,j,m,n;
Scanner sc=new Scanner(System.in);
System.out.println("enter no of rows & column");
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.println("Enter the first matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Enter the second matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[i][j]=sc.nextInt();
}
}
System.out.println("first matrix is=:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print("\t"+ a[i][j]);
}
System.out.println();
}
System.out.println("second matrix is=:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print("\t"+ b[i][j]);
}
System.out.println();
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}

System.out.println("Addition of two Matrix java is :");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print("\t"+ c[i][j]);
}
System.out.println();
}
}
}

\*OUTPUT


Comments

Popular posts from this blog

Sequential File Allocation

Indexed File Allocation method

Linked File Allocation