Menu BAR

FEEL FREE TO ASK ANY PROGRAM and please mention any error if you find it

12 Feb 2024

JAVA PROGRAM - ADDITION OF TWO MATRICES


 ## Write a Java Program to ADD TWO MATRICES





public class MatrixAddition { 
                public static void main(String[] args) { 

                                    int[][] firstMatrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; 
                                    int[][] secondMatrix = { {9, 8, 7}, {6, 5, 4}, {3, 2, 1} }; 

                                    // Creating a matrix to store the result 
                                    int[][] result = new int[firstMatrix.length][firstMatrix[0].length]; 

                                    // Adding corresponding elements of two matrices 
                                    for (int i = 0; i < firstMatrix.length; i++) { 
                                                    for (int j = 0; j < firstMatrix[i].length; j++) { 
                                                                    result[i][j] = firstMatrix[i][j] + secondMatrix[i][j]; 
                                                    
                                    

                                    // Displaying the result matrix 
                                    System.out.println("Result of Matrix Addition:"); 

                                    for (int i = 0; i < result.length; i++) { 
                                                            for (int j = 0; j < result[i].length; j++) { 
                                                                                System.out.print(result[i][j] + " "); 
                                                            
                                   System.out.println(); 
                                  
                
}

No comments:

Post a Comment