This is a Java Program to Add Two MXN Matrix from User Input.

Enter the elements of two arrays as input. Each element of the first matrix will added to the corresponding element in the second matrix and get the resultant matrix.

Here is the source code of the Java Program to Add Two MXN Matrix from User Input. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

import java.util.Scanner;

public class Add_Matrix

`{`

public static void main(String[] args)

`{`

int p, q, m, n;

Scanner s = new Scanner(System.in);

System.out.print("Enter number of rows in first matrix:");

p = s.nextInt();

System.out.print("Enter number of columns in first matrix:");

q = s.nextInt();

System.out.print("Enter number of rows in second matrix:");

m = s.nextInt();

System.out.print("Enter number of columns in second matrix:");

n = s.nextInt();

if (p == m && q == n)

`{`

int a[][] = new int[p][q];

int b[][] = new int[m][n];

int c[][] = new int[m][n];

System.out.println("Enter all the elements of first matrix:");

for (int i = 0; i < p; i++)

`{`

for (int j = 0; j < q; j++)

`{`

a[i][j] = s.nextInt();

`}`

`}`

System.out.println("Enter all the elements of second matrix:");

for (int i = 0; i < m; i++)

`{`

for (int j = 0; j < n; j++)

`{`

b[i][j] = s.nextInt();

`}`

`}`

System.out.println("First Matrix:");

for (int i = 0; i < p; i++)

`{`

for (int j = 0; j < q; j++)

`{`

System.out.print(a[i][j]+" ");

`}`

System.out.println("");

`}`

System.out.println("Second Matrix:");

for (int i = 0; i < m; i++)

`{`

for (int j = 0; j < n; j++)

`{`

System.out.print(b[i][j]+" ");

`}`

System.out.println("");

`}`

for (int i = 0; i < p; i++)

`{`

for (int j = 0; j < n; j++)

`{`

for (int k = 0; k < q; k++)

`{`

c[i][j] = a[i][j] + b[i][j];

`}`

`}`

`}`

System.out.println("Matrix after addition:");

for (int i = 0; i < p; i++)

`{`

for (int j = 0; j < n; j++)

`{`

System.out.print(c[i][j]+" ");

`}`

System.out.println("");

`}`

`}`

`else`

`{`

System.out.println("Addition would not be possible");

`}`

`}`

`}`

Output:

$ javac Add_Matrix.java $ java Add_Matrix Enter number of rows in first matrix:2 Enter number of columns in first matrix:3 Enter number of rows in second matrix:2 Enter number of columns in second matrix:3 Enter all the elements of first matrix: 1 2 3 4 5 6 Enter all the elements of second matrix: 7 8 9 4 3 2 First Matrix: 1 2 3 4 5 6 Second Matrix: 7 8 9 4 3 2 Matrix after addition: 8 10 12 8 8 8

**Sanfoundry Global Education & Learning Series – 1000 Java Programs.**

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

advertisement

advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

**Next Steps:**

- Get Free Certificate of Merit in Java Programming
- Participate in Java Programming Certification Contest
- Become a Top Ranker in Java Programming
- Take Java Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

**Related Posts:**

- Apply for Java Internship
- Practice Information Technology MCQs
- Practice Programming MCQs
- Apply for Information Technology Internship
- Buy Java Books