Addition of two matrix 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 development. The contributions for controlling robots are acquired based on the calculations from matrices.
The different operations on the matrix are also very important to us. There are different ways to perform matrix addition in Java.
Java Matrix Addition using for Loop
A nested loop is a loop inside a loop. The following Java program uses a nested for loop to iterate through each row and each column. In each iteration, we add the corresponding elements from the two matrices and store them in the result.
public class JavaMatrixAddition{
public static void main(String args[]){
//creating two matrices
int a[][]={{6,2,2},{5,2,1},{7,3,2}};
int b[][]={{6,2,2},{2,8,2},{8,2,6}};
//creating empty matrix to store the sum of two matrices
int c[][]=new int[3][3];
//adding matrices
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
Output of the above code:
12 4 4
7 10 3
15 5 8
Java Matrix Addition using while Loop
In the given Java program, we ask the user to enter the number of rows and columns and their elements, and then perform matrix addition using while loop.
import java.util.Scanner;
class JavaMatrixAddition
{
public static void main(String args[])
{
int row, col,i,j;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
row = in.nextInt();
System.out.println("Enter the number columns: ");
col = in.nextInt();
int matrix1[][] = new int[row][col];
int matrix2[][] = new int[row][col];
int result[][] = new int[row][col];
System.out.println("Enter the elements of matrix1: ");
i=0;
while(i < row)
{
j=0;
while(j < col)
{
matrix1[i][j] = in.nextInt();
j++;
}
i++;
}
System.out.println("Enter the elements of matrix2: ");
i=0;
while(i < row)
{
j=0;
while(j < col)
{
matrix2[i][j] = in.nextInt();
j++;
}
i++;
}
i=0;
while ( i < row)
{
j=0;
while(j < col)
{
result[i][j] = matrix1[i][j] + matrix2[i][j] ;
j++;
}
i++;
}
System.out.println("Sum of matrices: ");
i=0;
while(i < row)
{
j=0;
while(j < col)
{
System.out.print(result[i][j]+"\t");
j++;
}
System.out.println();
i++;
}
}
}
Output of the above code:
Enter the number of rows:
2
Enter the number columns:
2
Enter the elements of matrix1:
5 6
3 7
Enter the elements of matrix2:
3 9
5 2
Sum of matrices:
8 15
8 9
Java Matrix Addition using do while loop
In the given Java program, we have used the do while loop to perform the matrix addition.
import java.util.Scanner;
class JavaMatrixAddition
{
public static void main(String args[])
{
int row, col,i,j;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
row = sc.nextInt();
System.out.println("Enter the number columns: ");
col = sc.nextInt();
int matrix1[][] = new int[row][col];
int matrix2[][] = new int[row][col];
int res[][] = new int[row][col];
System.out.println("Enter the elements of matrix1: ");
i=0;
do
{
j=0;
do
{
matrix1[i][j] = sc.nextInt();
j++;
}while(j < col);
i++;
} while ( i < row);
System.out.println("Enter the elements of matrix2: ");
i=0;
do
{
j=0;
do
{
matrix2[i][j] = sc.nextInt();
j++;
}while(j < col);
i++;
} while ( i < row);
i=0;
do
{
j=0;
do
{
res[i][j] = matrix1[i][j] + matrix2[i][j] ;
j++;
}while(j < col);
i++;
}while ( i < row);
System.out.println("Sum of matrices: ");
i=0;
do
{
j=0;
do
{
System.out.print(res[i][j]+"\t");
j++;
}while(j < col);
System.out.println();
i++;
}while(i< row);
}
}
Output of the above code:
Enter the number of rows: 2
Enter the number columns: 2
Enter the elements of matrix1:
2 9
9 6
Enter the elements of matrix2:
8 5
7 9
Sum of matrices:
10 14
16 15
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