22:40

Add Array

Posted by Mitesh Patel |


import java.util.Scanner;

class AddArray {

    public static void main(String args[]) {

        int row, col;
        int a[][] = new int[3][3];
        int b[][] = new int[3][3];
        System.out.println("Enter Array A");
        //scan data for a
        for (row = 0; row < 3; row++) {
            for (col = 0; col < 3; col++) {
                a[row][col] = new Scanner(System.in).nextInt();
            }
        }
        System.out.println("Enter Array B");

        //scan data for b
        for (row = 0; row < 3; row++) {
            for (col = 0; col < 3; col++) {
                b[row][col] = new Scanner(System.in).nextInt();
            }
        }

        System.out.println("View Array A");

        for (row = 0; row < 3; row++) {
            for (col = 0; col < 3; col++) {
                System.out.print(a[row][col] + "\t");
            }
            System.out.println("");


        }
        System.out.println("View Array B");

        for (row = 0; row < 3; row++) {
            for (col = 0; col < 3; col++) {
                System.out.print(b[row][col] + "\t");
            }
            System.out.println("");


        }

        System.out.println("Sum");

        for (row = 0; row < 3; row++) {
            for (col = 0; col < 3; col++) {
                System.out.print(a[row][col] + b[row][col] + "\t");
            }
            System.out.println("");


        }

    }
}

0 comments:

Post a Comment

Subscribe