Java: Add two matrices of the same size
Java Array: Exercise-19 with Solution
Write a Java program to add two matrices of the same size.
Pictorial Presentation:
Sample Solution:
Java Code :
// Import the Java utility for reading input.
import java.util.Scanner;
// Define a class named Exercise19.
public class Exercise19 {
public static void main(String args[]) {
int m, n, c, d;
// Create a new Scanner object to read user input.
Scanner in = new Scanner(System.in);
// Prompt the user to input the number of rows for the matrix.
System.out.println("Input number of rows of the matrix");
m = in.nextInt();
// Prompt the user to input the number of columns for the matrix.
System.out.println("Input number of columns of the matrix");
n = in.nextInt();
// Create two-dimensional arrays to store matrix data.
int array1[][] = new int[m][n];
int array2[][] = new int[m][n];
int sum[][] = new int[m][n];
// Prompt the user to input elements of the first matrix.
System.out.println("Input elements of the first matrix");
for (c = 0; c < m; c++) {
for (d = 0; d < n; d++) {
array1[c][d] = in.nextInt();
}
}
// Prompt the user to input elements of the second matrix.
System.out.println("Input elements of the second matrix");
for (c = 0; c < m; c++) {
for (d = 0; d < n; d++) {
array2[c][d] = in.nextInt();
}
}
// Calculate the sum of the matrices.
for (c = 0; c < m; c++) {
for (d = 0; d < n; d++) {
sum[c][d] = array1[c][d] + array2[c][d];
}
}
// Display the result, which is the sum of the matrices.
System.out.println("Sum of the matrices:");
for (c = 0; c < m; c++) {
for (d = 0; d < n; d++) {
System.out.print(sum[c][d] + "\t");
}
System.out.println();
}
}
}
Sample Output:
Input number of rows of matrix 2 Input number of columns of matrix 2 Input elements of first matrix 1 2 3 4 Input the elements of second matrix 5 6 7 8 Sum of the matrices:- 6 8 10 12
Flowchart:
Java Code Editor:
Previous: Write a Java program to find the second smallest element in an array.
Next: Write a Java program to convert an array to ArrayList.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics