w3resource

Java: Add two matrices of the same size


19. Add two same-size matrices

Write a Java program to add two matrices of the same size.

Pictorial Presentation:

Java Array Exercises:  Add two matrices of the same size

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:

Flowchart: Java exercises: Add two matrices of the same size

For more Practice: Solve these Related Problems:

  • Write a Java program to add two matrices of different sizes by padding the smaller matrix with zeros.
  • Write a Java program to add two matrices without using nested loops.
  • Write a Java program to add two matrices and store the result in a third matrix.
  • Write a Java program to perform matrix addition recursively.

Go to:


PREV : Find second smallest array element.
NEXT : Convert array to ArrayList.

Java Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.