Hi All, hope you are doing good.

Usually we think that, adding two matrix will be very easy and normally we ignore to refresh this program before attending interview. Even though concepts are same Interviewer may ask same program in many ways. So be prepared while attending interview.

In this article explained clearly about two way interviewer might ask same question in two ways. Hope this article will be useful.

## 1. How to add two matrix without scanner class?

Below program explains about Adding 2 Matrix Using Multidimensional Arrays In Java programming without scanner class.

Here input data are hard-coded. Basically it is not a best practice to implement but just for learning program concept.

public class AddTwoMatrix { public static void main(String[] args) { // Initializing 3*3 First array int[][] Fist = {{1, 2, 3}, {4, 5, 6},{4, 5, 6}}; // Initializing 3*3 Second array int[][] Second = {{10, 20, 30}, {40, 50, 60},{40, 50, 60}}; // Declaring 3*3 matrix to store result int[][] Sum = new int[3][3]; //Add two matrix and store in Final matrix Sum for(int i=0; i<3;i++){ for(int j=0; j<3;j++){ Sum[i][j] = Fist[i][j]+Second[i][j]; } } //Display Final matrix System.out.println("Final sum matrix is: "); for(int i=0; i<3;i++){ for(int j=0; j<3;j++){ System.out.print(Sum[i][j]+"\t"); } System.out.println(); } } }

```
Output:
Final sum matrix is:
11 22 33
44 55 66
44 55 66
```

## 2. How to add two matrix with scanner class?

Below program explains about Adding 2 Matrix Using Multidimensional Arrays In Java programming with scanner class.

Here input data are not hard-coded but it given externally during run time through the keyboard. It is the best way to implement program.

import java.util.Scanner; public class Add2MatrixWithScanner { public static void main(String[] args) { //Creating object for scanner class to get data from input Scanner scn = new Scanner(System.in); //Store number of rows and columns counts in variables rows, cols respectively. System.out.println("Enter No. of Rows Counts"); int rows = scn.nextInt(); System.out.println("Enter No. of Columns Counts"); int cols = scn.nextInt(); //Declare First, Second and Final matrix int[][] Fist = new int[rows][cols]; int[][] Second = new int[rows][cols]; int[][] Sum = new int[rows][cols]; //Get First matrix data from input System.out.println("Enter 1st matrix data"); for(int i=0; i<rows;i++){ for(int j=0; j<cols;j++){ Fist[i][j] = scn.nextInt(); } } //Get Second matrix data from input System.out.println("Enter 2nd matrix data"); for(int i=0; i<rows;i++){ for(int j=0; j<cols;j++){ Second[i][j] = scn.nextInt(); } } //Add two matrix and store in Final matrix Sum for(int i=0; i<rows;i++){ for(int j=0; j<cols;j++){ Sum[i][j] = Fist[i][j]+Second[i][j]; } } //Display Final matrix System.out.println("Final sum matrix is: "); for(int i=0; i<rows;i++){ for(int j=0; j<cols;j++){ System.out.print(Sum[i][j]+"\t"); } System.out.println(); } } }

```
Output:
Enter No. of Rows Counts
3
Enter No. of Columns Counts
3
Enter 1st matrix data
1
2
3
6
5
4
7
8
9
Enter 2nd matrix data
1
2
3
4
5
6
9
8
7
Final sum matrix is:
2 4 6
10 10 10
16 16 16
```

Recommended article for you,

- Introduction to java
- How to install java
- Check Armstrong number in java
- Check given number Prime or Not
- Sort an array in ascending, descending order in java

Hey, Now it is your time! Drop a comment if more details needed or if any update requires. Your comments are more valuable to improve our site to help others.

sirglio freiHaving read this I thought it was very informative. I appreciate you taking the time and effort to put this article together. I once again find myself spending way to much time both reading and commenting. But so what, it was still worth it!

protechbeachThank you frei

cbd oilHello, I enjoy reading through your post. I like to write

a little comment to support you.