Matrix Addition Java

PROGRAM TO ADD TWO MATRICES


  1. public class MatrixAdditionExample{  
  2. public static void main(String args[]){  
  3. //creating two matrices    
  4. int a[][]={{1,3,4},{2,4,3},{3,4,5}};    
  5. int b[][]={{1,3,4},{2,4,3},{1,2,4}};    
  6.     
  7. //creating another matrix to store the sum of two matrices    
  8. int c[][]=new int[3][3];  //3 rows and 3 columns  
  9.     
  10. //adding and printing addition of 2 matrices    
  11. for(int i=0;i<3;i++){    
  12. for(int j=0;j<3;j++){    
  13. c[i][j]=a[i][j]+b[i][j];    //use - for subtraction  
  14. System.out.print(c[i][j]+" ");    
  15. }    
  16. System.out.println();//new line    
  17. }    
  18. }}  


OUTPUT

2 6 8
4 8 6
4 6 9

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java