In this post of Java program to add two matrices we are going to learn how we can write a java program to add two matrices.
When we add two matrices addition is done index based we add the element at (0, 0) in the first matrix with the element at (0, 0) in the second matrix, element at (0, 1) in the first matrix with the element at (0, 1) in the second matrix and so on.
For example – If you are adding two matrices of order 3X3
Thus the resultant matrix is –
We should also remember these points when adding one matrix with another –
- Both of the matrices have to be of same size or dimension.
- Resultant matrix will also have the same order for the elements. Element at (0, 0) in the first matrix added with (0, 0) of the second matrix becomes the element at index (0, 0) in the resultant matrix too.
You may also like –
Find largest and smallest number in the given Array
Find duplicate elements in an Array
Java program to Remove Duplicate Elements From an Array
find common elements between two Arrays
java program to add two matrices
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
package com.kkjavatutorials.client; import java.util.Scanner; /** * @author KK JavaTutorials * Java Program to add two matrices */ public class MatrixAddition { public static void main(String[] args) { int numberOfRows; int numberOfcolumns; Scanner scanner = null; try { scanner = new Scanner(System.in); System.out.print("Enter number of rows of matrix: "); numberOfRows = scanner.nextInt(); System.out.print("Enter number of columns of matrix: "); numberOfcolumns = scanner.nextInt(); int Matrix1[][] = new int[numberOfRows][numberOfcolumns]; int Matric2[][] = new int[numberOfRows][numberOfcolumns]; int resultMatrix[][] = new int[numberOfRows][numberOfcolumns]; System.out.print("Enter elements of first matrix: "); for (int i = 0; i < numberOfRows; i++) { for (int j = 0; j < numberOfcolumns; j++) { Matrix1[i][j] = scanner.nextInt(); } } System.out.println("First Matrix is: "); for (int i = 0; i < numberOfRows; i++) { for (int j = 0; j < numberOfcolumns; j++) { System.out.print(" " + Matrix1[i][j] + "\t"); } System.out.println(); } System.out.print("Enter elements of Second Matrix: "); for (int i = 0; i < numberOfRows; i++) { for (int j = 0; j < numberOfcolumns; j++) { Matric2[i][j] = scanner.nextInt(); } } System.out.println("Second Matrix is: "); for (int i = 0; i < numberOfRows; i++) { for (int j = 0; j < numberOfcolumns; j++) { System.out.print(" " + Matric2[i][j] + "\t"); } System.out.println(); } //Addition logic of two matrices for (int i = 0; i < numberOfRows; i++) { for (int j = 0; j < numberOfcolumns; j++) { resultMatrix[i][j] = Matrix1[i][j] + Matric2[i][j]; } } //Printing the result matrix System.out.println("Result Matrix is: "); for (int i = 0; i < resultMatrix.length; i++) { for (int j = 0; j < numberOfcolumns; j++) { System.out.print(" " + resultMatrix[i][j] + "\t"); } System.out.println(); } } catch (Exception e) { e.printStackTrace(); }finally { if(scanner != null){ scanner.close(); } } } } |
Output of above program for 3X3 matrices:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
Enter number of rows of matrix: 3 Enter number of columns of matrix: 3 Enter elements of first matrix: 12 2 5 6 7 8 5 7 8 First Matrix is: 12 2 5 6 7 8 5 7 8 Enter elements of Second Matrix: 10 15 2 3 5 6 7 8 4 Second Matrix is: 10 15 2 3 5 6 7 8 4 Result Matrix is: 22 17 7 9 12 14 12 15 12 |
That’s all for this topic Java program to adds two matrices. If you have any questions or doubts or any suggestions to make please drop a comment.