
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add Two Matrices Using Multi-Dimensional Arrays in Java
For two given matrices, each of size m x n, we need to write a Java program to add them.
Amatrix has a row and column arrangement of its elements. A matrix with m rows and n columns can be called as m x n matrix. Individual entries in the matrix are called elements and can be represented by a[i][j], which means that the element a is present at the ith row and jth column.
Matrix is an example of multi-dimensional array. In Java, a multi-dimensional array is an array of arrays. It is used to store data within a table, grid, or matrix having rows and columns.
Note that two matrices can be added if and only if the number of rows and columns of each matrix is equal. Now, let's understand the problem statement with an example scenario.
Example Scenario:
Input 1: matrix1 = {{2, 3, 4}, {5, 2, 3}, {4, 6, 9}} Input 2: matrix2 = {{1, 5, 3}, {5, 6, 3}, {8, 1, 5}} Output: res = {{3 8 7}, {10 8 6}, {12 7 14}}
How to add two Matrices in Java?
In Java, we can use the following ways to add two matrices:
Using a Nested for Loop
To add to matrices we need to iterate over each element of the both matrices using outer for-loop and add the element at [i][j] position of the first matrix with the element at [i][j] position of the second matrix using inner for-loop and store the value at [i][j] position of the resultant matrix.
Example
In this Java program, we use a nested for loop to add two matrices -
public class AddMatrices { public static void main(String[] args) { int matrix_size = 3; int[][] input_matrix_1 = { {2, 3, 4}, {5, 2, 3}, {4, 6, 9} }; System.out.println("The first matrix is defined as: \n"); for (int i = 0; i < matrix_size; i++) { for (int j = 0; j < matrix_size; j++) { System.out.print(input_matrix_1[i][j] + " "); } System.out.println(); } int[][] input_matrix_2 = { {1, 5, 3}, {5, 6, 3}, {8, 1, 5} }; System.out.println("The second matrix is defined as: \n"); for (int i = 0; i < matrix_size; i++) { for (int j = 0; j < matrix_size; j++) { System.out.print(input_matrix_2[i][j] + " "); } System.out.println(); } int[][] resultant_matrix = new int[matrix_size][matrix_size]; for(int i = 0; i < matrix_size; i++) { for (int j = 0; j < matrix_size; j++) { resultant_matrix[i][j] = input_matrix_1[i][j] + input_matrix_2[i][j]; } } System.out.println("The sum of the two matrices is: "); for(int[] row : resultant_matrix) { for (int column : row) { System.out.print(column + " "); } System.out.println(); } } }
Output of the above code is as:
The first matrix is defined as: 2 3 4 5 2 3 4 6 9 The second matrix is defined as: 1 5 3 5 6 3 8 1 5 The sum of the two matrices is: 3 8 7 10 8 6 12 7 14
Using Nested while Loop
This is another way of adding two matrices in Java. Here, we use the nested while loop in place of the nested for loop. The implementation uses the same logic as the previous example.
Example
Here, we use a nested while loop to add two matrices in Java -
public class AddMatrices { public static void main(String args[]) { int mtr1[][] = { {1, 7}, {9, 2} }; int mtr2[][] = { {6, 3}, {5, 1} }; int add[][] = new int[2][2]; int i=0; while(i < 2) { int j = 0; while(j < 2) { add[i][j] = mtr1[i][j] + mtr2[i][j]; j++; } i++; } System.out.println("Sum of given two matrices = "); i = 0; while(i < 2) { int j = 0; while(j < 2) { System.out.print(add[i][j] + "\t"); j++; } System.out.println(); i++; } } }
On running the above code, it will show the following output -
Sum of given two matrices = 7 10 14 3