import java.util.Scanner;
class AddTwoMatrix {
  public static void main(String args[]) {
    int m, n, c, d;
    // Create scanner object to take input
    Scanner in = new Scanner(System.in);
    // Taking number of rows and columns as input
    System.out.println("Enter the number of rows and columns of matrix");
    m = in.nextInt();
    n = in.nextInt();
    // Declare matrices
    int first[][] = new int[m][n];
    int second[][] = new int[m][n];
    int sum[][] = new int[m][n];
    // Taking elements of first matrix as input
    System.out.println("Enter the elements of first matrix");
    for (c = 0; c < m; c++) {
      for (d = 0; d < n; d++) {
        first[c][d] = in.nextInt();
      }
    }
    // Taking elements of second matrix as input
    System.out.println("Enter the elements of second matrix");
    for (c = 0; c < m; c++) {
      for (d = 0; d < n; d++) {
        second[c][d] = in.nextInt();
      }
    }
    // Add the two matrices
    for (c = 0; c < m; c++) {
      for (d = 0; d < n; d++) {
        sum[c][d] = first[c][d] + second[c][d]; // replace '+' with '-' to subtract matrices
      }
    }
    // Display the sum of matrices
    System.out.println("Sum of entered matrices:");
    for (c = 0; c < m; c++) {
      for (d = 0; d < n; d++) {
        System.out.print(sum[c][d] + "\t");
      }
      System.out.println();
    }
    // Close the scanner to prevent resource leak
    in.close();
  }
}
Step-by-Step Explanation:
1. Importing Scanner Class:
import java.util.Scanner;
This imports the Scanner class from java.util to read from the user.
2. Class Definition:
class AddTwoMatrix
A class AddTwoMatrix is declared that contains the main method from where the program execution starts.
3. Main Method:
public static void main(String args[])
The main method is the entry point of the program. The program starts executing from here.
4. Declaring Variables:
int m, n, c, d;
Scanner in = new Scanner(System.in);
- m and n are variables to store the number of rows and columns of the matrices.
 - c and d are loop counters for traversing the rows and columns of the matrices.
 - Scanner in = new Scanner(System.in) is a Scanner object named in to read user input.
 
5. Input the Matrix Dimensions:
System.out.println(“Enter the number of rows and columns of matrix”);
m = in.nextInt();
n = in.nextInt();
- The program asks the user to input the number of rows (m) and columns (n) for the matrices.
 - The user enters the values for m and n.
 
6. Matrix Declaration:
int first[][] = new int[m][n];
int second[][] = new int[m][n];
int sum[][] = new int[m][n];
Three 2D arrays are declared:
- first[][]: To hold elements of the first matrix.
 - second[][]: To hold elements of the second matrix.
 - sum[][]: To hold the sum of the two matrices.
 
7. Input the Elements of the First Matrix:
System.out.println(“Enter the elements of first matrix”);
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
first[c][d] = in.nextInt();
- The program asks the user to input the elements of the first matrix.
 - Two nested for loops are used:
- The outer loop (c) loops through rows.
 - The inner loop (d) loops through the column.
 
 
The elements of the first matrix are given from the keyboard that is located at first[c][d].
8. Enter the Elements of Second Matrix:
System.out.println(“Enter the elements of second matrix”);
for (c = 0; c < m; c++)
for (d = 0; d < n; d{})
second[c][d] = in.nextInt();
The above program asks for elements of the second matrix
Stored in the second array using nested for loops: for (c=0; c<m;c++)
for(d=0; d<n; d++)
9. Adding the Two Matrices:
for (c = 0; c < m; c)
for (d = 0; d < n; d++)
sum[c][d] = first[c][d] + second[c][d];
- A nested for loop is employed to add up the corresponding elements of the two matrices, viz. first[c][d] and second[c][d].
 - The addition of the elements is stored in the sum[c][d] matrix.
 
10. Print out the Final Sum Matrix:
System.out.println(“Sum of entered matrices:-“);
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
System.out.print(sum[c][d] + “\t”);
System.out.println()
- The program prints the sum of the matrices in a matrix format.
 - The outer loop (c) iterates through the rows, and the inner loop (d) iterates through the columns.
 - The sum of each element is printed, followed by a tab (\t) for formatting.
 
Sample Input and Output:
Sample 1:
Input:
Enter the number of rows and columns of matrix
2 2
Enter the elements of first matrix
1 2
3 4
Enter the elements of second matrix
5 6
7 8
Output:
Sum of entered matrices:-
6 8
10 12
Explanation:
Matrix 1:
1 2
3 4
Matrix 2:
5 6
7 8
Sum of the matrices:
(1+5) (2+6) –> 6 8
(3+7) (4+8) –> 10 12
Example 2:
Input:
Enter the number of rows and columns of matrix
3 3
Enter the elements of first matrix
1 1 1
1 1 1
1 1 1
Enter the elements of second matrix
2 2 2
2 2 2
2 2 2
Output:
Sum of entered matrices:-
3 3 3 3
3 3 3 3
3 3 3 3
Key Points:
- Matrix Input: It takes input of elements for two matrices using nested loops.
 - Matrix Addition: Adds corresponding elements of the two matrices and stores its value in the matrix sum[][].
 - Matrix Output: The sum is displayed as a matrix for better readability using tab separation.
 
Time Complexity:
The time complexity for this program is (O(m times n)), where m is the number of rows and n is the number of columns, since we have to scan each element in the matrices once.
Space Complexity:
The space complexity is also (O(m times n)) since we have to store three matrices of size (m times n).
