| 3 | import java.util.Scanner; |
| 4 | public class MatrixAdd { |
| 5 | public static void main(String[] args) { |
| 6 | // TODO Auto-generated method stub |
| 7 | Scanner sc = new Scanner(System.in); |
| 8 | int arr1[][] = new int[10][10]; // New 2D array for first matrix |
| 9 | int arr2[][] = new int[10][10]; // New 2D array for second matrix |
| 10 | int sum[][] = new int[10][10]; // New 2D array for sum of the two matrices |
| 11 | int i, j; |
| 12 | // Taking details of first matrix - |
| 13 | System.out.println("Enter details for first matrix: "); |
| 14 | for (i = 0; i < 2; i++) { |
| 15 | for (j = 0; j < 2; j++) { |
| 16 | System.out.println("Enter number: "); |
| 17 | arr1[i][j] = sc.nextInt(); |
| 18 | } |
| 19 | } |
| 20 | // Taking details of second matrix - |
| 21 | System.out.println("Enter details for second matrix: "); |
| 22 | for (i = 0; i < 2; i++) { |
| 23 | for (j = 0; j < 2; j++) { |
| 24 | System.out.println("Enter number: "); |
| 25 | arr2[i][j] = sc.nextInt(); |
| 26 | } |
| 27 | } |
| 28 | System.out.println("Displaying addition of two matrices: "); |
| 29 | // Performing addition of two matrices - |
| 30 | for (i = 0; i < 2; i++) { |
| 31 | for (j = 0; j < 2; j++) { |
| 32 | sum[i][j] = arr1[i][j] + arr2[i][j]; |
| 33 | } |
| 34 | } |
| 35 | // Displaying addition of two matrices - |
| 36 | for (i = 0; i < 2; i++) { |
| 37 | for (j = 0; j < 2; j++) { |
| 38 | System.out.print(sum[i][j] + " "); |
| 39 | } |
| 40 | System.out.println(); |
| 41 | } |
| 42 | } |
| 43 | } |