w3resource

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++ )
            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

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

Different between parseInt() and valueOf() in java?

Well, the API for Integer.valueOf(String) does indeed say that the String is interpreted exactly as if it were given to Integer.parseInt(String). However, valueOf(String) returns a new Integer() object whereas parseInt(String) returns a primitive int.

If you want to enjoy the potential caching benefits of Integer.valueOf(int), you could also use this eyesore:

Integer k = Integer.valueOf(Integer.parseInt("123"))

Now, if what you want is the object and not the primitive, then using valueOf(String) may be more attractive than making a new object out of parseInt(String) because the former is consistently present across Integer, Long, Double, etc.

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