Q:

Java Program to add two matrices

belongs to collection: Java Matrix Programs

0

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.

To subtract two matrices, use - operator. Let's see a simple example to add two matrices of 3 rows and 3 columns

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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    
}    
}}  

 

Output:

2 6 8
4 8 6
4 6 9

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Program to multiply two matrices... >>
<< Java Program to determine whether a given matrix i...