Write a java program to add two matrix

Write a java program to add two matrix

Write a java program to add two matrix

Program
public class Main
{
    public static void main(String args[])
    {
        int[][] a = new int[][] { { 1, 2, 3},{ 4, 5, 6},{ 7, 8, 9} };
        int[][] b = new int[][] { { 10, 11, 12},{ 13, 14, 15},{ 16, 17, 18} };
        int[][] c = new int[3][3];
        if(a.length == b.length && a[0].length == b[0].length)
        {
            for(int i = 0;i < a.length;i++)
            {
                for(int j = 0;j < a[i].length;j++)
                {
                    c[i][j] = a[i][j] + b[i][j];
                }
            }
        
        }
        else
        {
        System.out.println("'A' and 'B' Matrix are not SAME");
        return;
        }
        
        System.out.println("The Matrix 'A' Value:");
        for(int i = 0;i < a.length;i++)
        {
            for(int j = 0;j < a[i].length;j++)
            {
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("The Matrix 'B' Value:");
        for(int i = 0;i < a.length;i++)
        {
            for(int j = 0;j < a[i].length;j++)
            {
                System.out.print(b[i][j]+ " ");
            }
            System.out.println();
        }
        System.out.println("The Addition Matrix of 'A' and 'B' Value:");
        for(int i = 0;i < a.length;i++)
        {
            for(int j = 0;j < a[i].length;j++)
            {
                System.out.print(c[i][j] + " ");
            }
            System.out.println();
        }
    }
}


Output:

The Matrix 'A' Value:
1 2 3
4 5 6
7 8 9
The Matrix 'B' Value:
10 11 12
13 14 15
16 17 18
The Addition Matrix of 'A' and 'B' Value:
11 13 15
17 19 21
23 25 27



More Questions


45 . Write a java Program to print odd and even numbers from an array
46 . Write a java program to add two matrix
47 . Write a java Program to check given matrix is null matrix
48 . Add all the digits of a given positive integer until the result has a single digit
49 . Write a Java program to accept a float value of number and return a rounded float value



For latest job updates join Telegram Channel: https://t.me/sateeshm