1// Aim: Program to perform Matrix Addition.
2import java.util.Scanner;
3public class MatrixAddition {
4 public static void main(String[] args) {
5 Scanner sc = new Scanner(System.in);
6 System.out.print("Enter the number of rows: ");
7 int r = sc.nextInt();
8 System.out.print("Enter the number of columns: ");
9 int c = sc.nextInt();
10 int a[][] = new int[r][c];
11 int b[][] = new int[r][c];
12 int sum[][] = new int[r][c];
13 System.out.println("Enter the elements of the first matrix: ");
14 for(int i=0;i<r;i++)
15 {
16 for(int j=0;j<c;j++)
17 {
18 a[i][j] = sc.nextInt();
19 }
20 }
21 System.out.println("Enter the elements of the second matrix: ");
22 for(int i=0;i<r;i++)
23 {
24 for(int j=0;j<c;j++)
25 {
26 b[i][j] = sc.nextInt();
27 }
28 }
29 for(int i=0;i<r;i++)
30 {
31 for(int j=0;j<c;j++)
32 {
33 sum[i][j] = a[i][j] + b[i][j];
34 }
35 }
36 System.out.println("The sum of the two matrices is: ");
37 for(int i=0;i<r;i++)
38 {
39 for(int j=0;j<c;j++)
40 {
41 System.out.print(sum[i][j]+" ");
42 }
43 System.out.println();
44 }
45 sc.close();
46 }
47}
48