Matrix addition in Java
In this post, you will learn different ways to perform matrix addition using the Java programming language.
Matrix, a set of numbers arranged in rows and columns so as to form a rectangular array. Matrices are utilized substantially more in everyday life than individuals would have suspected. A square matrix can represent a linear transformation of a geometric object. A real-life example is Adobe Photoshop, it uses matrices to process linear transformations to render images. In robotics and automation, matrices are the fundamental segments for the robot developments. The contributions for controlling robots are acquired depending on the calculations from matrices.
The different operations on the matrix are also very important for us. There are different ways to perform matrix addition in Java.
Matrix Addition using Nested Loop
A nested loop is a loop inside a loop. The following program uses a nested for loop to iterate through each row and each column. In each iteration, we add the corresponding elements in the two matrices and store it in the result.
// Java Program to add two matrices using nested loop
public class MatrixAddition{
public static void main(String args[]){
//Initializing two matrices
int a[][]={{4,6,2},{9,8,2},{9,9,2}};
int b[][]={{2,4,1},{5,3,4},{2,1,7}};
//creating another matrix to store the sum of two matrices
int result[][]=new int[3][3]; //3 rows and 3 columns
// adding 2 matrices and displaying the result
// iterating through rows
for(int i=0;i<3;i++){
// iterating through columns
for(int j=0;j<3;j++){
result[i][j]=a[i][j]+b[i][j];
System.out.print(result[i][j]+" ");
}
System.out.println();
}
}
}
Output of the above code:
6 10 3
14 11 6
11 10 9
Matrix Addition using While loop
In the given example, we have used the while loop to add two matrices using the Java programming language.
// Java Program to add two matrices using while loop
import java.util.Scanner;
class MatrixAddition
{
public static void main(String args[])
{
int rows, cols,i,j;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows : ");
rows = in.nextInt();
System.out.println("Enter the number columns : ");
cols = in.nextInt();
int matrix1[][] = new int[rows][cols];
int matrix2[][] = new int[rows][cols];
int result[][] = new int[rows][cols];
System.out.println("Enter the elements of matrix1 : ");
i=0;
while ( i < rows)
{
j=0;
while(j < cols)
{
matrix1[i][j] = in.nextInt();
j++;
}
i++;
}
System.out.println("Enter the elements of matrix2 : ");
i=0;
while ( i < rows)
{
j=0;
while(j < cols)
{
matrix2[i][j] = in.nextInt();
j++;
}
i++;
}
i=0;
while ( i < rows)
{
j=0;
while(j < cols)
{
result[i][j] = matrix1[i][j] + matrix2[i][j] ;
j++;
}
i++;
}
System.out.println("Sum of matrices: ");
i=0;
while ( i < rows)
{
j=0;
while(j < cols)
{
System.out.print(result[i][j]+"\t");
j++;
}
System.out.println();
i++;
}
}
}
Output of the above code:
Enter the number of rows : 3
Enter the number columns : 3
Enter the elements of matrix1 :
5 4 5
2 5 2
7 5 2
Enter the elements of matrix2 :
7 3 6
2 7 3
6 2 2
Sum of matrices:
12 7 11
4 12 5
13 7 4
Using do-while loop
In the given program, we have used the do-while loop to find the addition of two matrices.
// Java Program to add two matrices using nested do-while loop
import java.util.Scanner;
class MatrixAddition
{
public static void main(String args[])
{
int rows, cols,i,j;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows : ");
rows = in.nextInt();
System.out.println("Enter the number columns : ");
cols = in.nextInt();
int matrix1[][] = new int[rows][cols];
int matrix2[][] = new int[rows][cols];
int result[][] = new int[rows][cols];
System.out.println("Enter the elements of matrix1 : ");
i=0;
do
{
j=0;
do
{
matrix1[i][j] = in.nextInt();
j++;
}while(j < cols);
i++;
} while ( i < rows);
System.out.println("Enter the elements of matrix2 : ");
i=0;
do
{
j=0;
do
{
matrix2[i][j] = in.nextInt();
j++;
}while(j < cols);
i++;
} while ( i < rows);
i=0;
do
{
j=0;
do
{
result[i][j] = matrix1[i][j] + matrix2[i][j] ;
j++;
}while(j < cols);
i++;
}while ( i < rows);
System.out.println("Sum of matrices :");
i=0;
do
{
j=0;
do
{
System.out.print(result[i][j]+"\t");
j++;
}while(j < cols);
System.out.println();
i++;
}while(i< rows);
}
}
Output of the above code:
Enter the number of rows : 3
Enter the number columns : 3
Enter the elements of matrix1 :
6 2 8
2 4 3
8 5 2
Enter the elements of matrix2 :
8 3 6
2 8 3
6 3 4
Sum of matrices :
14 5 14
4 12 6
14 8 6
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java