package Prog;
import java.util.Scanner;
public class AddMatrix
{
public static void main(String a[])
{
System.out.println("Enter the rows and columns of matrix");
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
int n=sc.nextInt();
int first[][]=new int[m][n];
int second[][]=new int[m][n];
int sum[][]=new int[m][n];
System.out.println("Enter the first matrix");
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
first[i][j]=sc.nextInt();
}
}
System.out.println("Enter the second matrix");
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
second[i][j]=sc.nextInt();
}
}
System.out.println("Sum of matrix");
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
sum[i][j]=first[i][j]+second[i][j];
System.out.print(sum[i][j]+"\t");
}
}
}
}
Output:
-------------------------------------------------------------------------------------------------------------------------
Enter the rows and columns of matrix
2
2
Enter the first matrix
1
1
2
2
Enter the second matrix
3
3
4
4
Sum of matrix
4 4 6 6
0 Comments