Answers for "add two matrices of the same size in java"

0

add two matrices of the same size in java

public class AddTwoMatrices {

    public static void main(String[] args) {

        int r, c, i, j;
        Scanner in = new Scanner(System.in);

        System.out.println("Enter the row size of the matrix:");
        r = in.nextInt();

        System.out.println("Enter the column size of the matrix:");
        c = in.nextInt();

        int array1[][] = new int[r][c];
        int array2[][] = new int[r][c];
        int sum[][] = new int[r][c];

        System.out.println("Input elements of the first matrix:");
        for (i = 0; i < r; i++) {
            for (j = 0; j < c; j++) {
                array1[i][j] = in.nextInt();
            }
        }

        System.out.println("Input elements of the second matrix:");
        for (i = 0; i < r; i++) {
            for (j = 0; j < c; j++) {
                array2[i][j] = in.nextInt();
            }
        }

        for (i = 0; i < array1.length; i++) {
            for (j = 0; j < array2.length; j++) {
                sum[i][j] = array1[i][j] + array2[i][j];
                System.out.print(sum[i][j] + "\t");
            }
            System.out.println();
        }
    }
}
Posted by: Guest on June-26-2021

Code answers related to "add two matrices of the same size in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language