Java Array Exercises: 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:

Java Array Exercises:  Add two matrices of the same size

Sample Solution:

Java Code :

import java.util.Scanner;
public class Exercise19 {
 public static void main(String args[])
      int m, n, c, d;
      Scanner in = new Scanner(System.in);
      System.out.println("Input number of rows of matrix");
      m = in.nextInt();
      System.out.println("Input number of columns of matrix");
      n  = in.nextInt();
      int array1[][] = new int[m][n];
      int array2[][] = new int[m][n];
      int sum[][] = new int[m][n];
      System.out.println("Input elements of first matrix");
      for (  c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
            array1[c][d] = in.nextInt();
      System.out.println("Input the elements of second matrix");
      for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
            array2[c][d] = in.nextInt();
      for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
             sum[c][d] = array1[c][d] + array2[c][d]; 
      System.out.println("Sum of the matrices:-");
      for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )

Sample Output:

Input number of rows of matrix                                                                                
Input number of columns of matrix                                                                             
Input elements of first matrix                                                                                
Input the elements of second matrix                                                                           
Sum of the matrices:-                                                                                         
6       8                                                                                                
10      12 


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

Java Code Editor:

Improve this sample solution and post your code through Disqus

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?

Java: Tips of the Day

Array vs ArrayLists:

The main difference between these two is that an Array is of fixed size so once you have created an Array you cannot change it but the ArrayList is not of fixed size. You can create instances of ArrayLists without specifying its size. So if you create such instances of an ArrayList without specifying its size Java will create an instance of an ArrayList of default size.

Once an ArrayList is full it re-sizes itself. In fact, an ArrayList is internally supported by an array. So when an ArrayList is resized it will slow down its performance a bit as the contents of the old Array must be copied to a new Array.

At the same time, it's compulsory to specify the size of an Array directly or indirectly while creating it. And also Arrays can store both primitives and objects while ArrayLists only can store objects.

Ref: https://bit.ly/3o8L2KH