Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Matrix Addition

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Matrix Addition

We can add two matrices in java using binary + operator. A matrix is also known as array of arrays.
We can add, subtract and multiply matrices.
Java program to add two matrices – The following Java Code will let you know how to perform two matrix
addition using Java. We can write the Program in three Possible ways. Here it is,

•Using For Loop


• Using While
• Using Do-While

Addition of two matrices can be carried if and only if both the matrices are in the same order. In matrix addition,
each term of one matrix is added to the other matrix’s term, at the same location, i.e. the term at first row first
column of Matrix 1 will be added to the term at first row first column of Matrix 2 and so on. A pictorial example is
given below.
Addition Of Two Matrices – Using For Loop
1) If both matrices are of the same size then only we can add the matrices.

2) Use the double dimensional array to store the matrix elements.

3) Read row number, column number and initialize the double dimensional arrays
mat1[][],mat2[][],res[][] with same row number, column number.

4)Store the first matrix elements into the two-dimensional array mat1[][] using two for
loops. i indicates row number, j indicates column index. Similarly matrix 2 elements in to
mat2[][].

5)Add the two matrices using for loop

for i=0 to i<row


for j=0 to j<col
mat1[i][j] + mat2[i][j] and store it in to the matrix res at res[i][j] .
public class MatrixAdditionExample{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,3,4},{2,4,3},{3,4,5}};
int b[][]={{1,3,4},{2,4,3},{1,2,4}};

//creating another matrix to store the sum of two matrices


int c[][]=new int[3][3]; //3 rows and 3 columns

//adding and printing addition of 2 matrices


for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j]; //use - for subtraction
System.out.print(c[i][j]+" ");
}
System.out.println();//new line
}
}
}
import java.util.Scanner;
System.out.println("Enter the elements of matrix1");
class AddMatrix
for ( i= 0 ; i < row ; i++ )
{
{
public static void main(String args[])
{
for ( j= 0 ; j < col ;j++ )
int row, col,i,j;
mat1[i][j] = in.nextInt();
Scanner in = new Scanner(System.in);
System.out.println();
System.out.println("Enter the number of rows");
}
row = in.nextInt();
System.out.println("Enter the elements of matrix2");
System.out.println("Enter the number columns");
for ( i= 0 ; i < row ; i++ )
col = in.nextInt();
{
int mat1[][] = new int[row][col];
for ( j= 0 ; j < col ;j++ )
int mat2[][] = new int[row][col];
mat2[i][j] = in.nextInt();
int res[][] = new int[row][col];
System.out.println();
}
for ( i= 0 ; i < row ; i++ ) Output
for ( j= 0 ; j < col ;j++ ) Enter the number of rows
res[i][j] = mat1[i][j] + mat2[i][j] ; 2
Enter the number columns
System.out.println("Sum of matrices:-"); 2
Enter the elements of matrix1
for ( i= 0 ; i < row ; i++ ) 11
{
for ( j= 0 ; j < col ;j++ ) 11
System.out.print(res[i][j]+"\t");
Enter the elements of matrix2
System.out.println(); 22
}
22
}
} Sum of matrices:-
3 3
3 3
Using While Loop

1) For insert values in the matrices mat1, mat2 using loops –

While loop iterates until i <row


while iterates until j<column
insert the value at mat1[i][j] and increases the j value.
increase the i value.
2) To add matrices –

while loop iterates until i<row


while loop iterates until j<column
add mat1[i][j] + mat2[i][j] and insert the result at res[i][j].
increase the j value.
increase the i value.
3) Print res[i][j] using two for loops then we will get the addition of two matrices.
import java.util.Scanner;
class MatrixAddition System.out.println("Enter the elements of first
{ matrix: ");
public static void main(String args[]) int i = 0 ;
{ int j = 0 ;
int row, column; while ( i <row ){
Scanner in = new Scanner(System.in); j=0;
System.out.println("Enter the number of rows and columns of while ( j <column ) {
matrix: "); matrixA[i][j] = in.nextInt();
row = in.nextInt(); j++;
column = in.nextInt(); }
int matrixA[][] = new int[row][column]; i++;
int matrixB[][] = new int[row][column]; }
int sumMatrix[][] = new int[row][column];
System.out.println("Enter the elements of second matrix: ");
i=0;
while ( i <row ){
j=0;
while ( j <column ) { i=0;
matrixB[i][j] = in.nextInt(); while ( i <row ){
j++; j=0;
} while ( j <column ) {
i++; System.out.print(sumMatrix[i][j]+"\t");
} j++;
}
System.out.println("Sum of Matrix"); System.out.print("\n");
i=0; i++;
while ( i <row ){ }
j=0; }
while ( j <column ) { }
sumMatrix[i][j] = matrixA[i][j] + matrixB[i][j];
j++;
}
i++;
}
Output

Enter the number of rows and columns of matrix:


22
Enter the elements of first matrix:
56
78
Enter the elements of second matrix:
12
34
Sum of Matrix
6 8
10 12
Using Do-While Loop

1) For Addition of two matrices

i=0
do loop
j=0
do loop
mat1[i][j] + mat2[i][j] and insert the result in to res[i][j].
Increase the j value. checks the condition j<col.
Increase the i value. Then checks the condition j<row.
2) Print res[][] then we will get the addition of two matrices.
import java.util.Scanner;
System.out.println("Enter the elements of matrix1");
class AddMatrix i=0;
{ do
public static void main(String args[]) {
{ j=0;
int row, col,i,j; do
Scanner in = new Scanner(System.in); {
mat1[i][j] = in.nextInt();
System.out.println("Enter the number of rows"); j++;
row = in.nextInt(); }while(j < col);
i++;
System.out.println("Enter the number columns");
col = in.nextInt(); } while ( i < row);

int mat1[][] = new int[row][col];


int mat2[][] = new int[row][col];
int res[][] = new int[row][col];
System.out.println("Enter the elements of matrix2"); i=0;
i=0; do
do {
{ j=0;
j=0; do
do {
{
mat2[i][j] = in.nextInt(); res[i][j] = mat1[i][j] + mat2[i][j] ;
j++; j++;
}while(j < col); }while(j < col);
i++; i++;

} while ( i < row); }while ( i < row);


System.out.println("Sum of matrices:-"); Output
i=0;
do
Enter the number of rows
{
3
j=0;
Enter the number columns
do
3
{
Enter the elements of matrix1
111
System.out.print(res[i][j]+"\t");
111
j++;
111
}while(j < col);
Enter the elements of matrix2
System.out.println();
999
i++;
999
999
}while(i< row);
Sum of matrices:-
10 10 10
}
10 10 10
}
10 10 10

You might also like