Welcome to ICSE Java Answers. Ask any questions you have related to Java and we will answer them.
Java is a registered trademark of Oracle. This site is in no way related to or endorsed by Oracle.

Accept 2D array and find sum of elements

0 votes
Write a program to input and store integer elements in a double dimensional array of  size 4×4 and find the sum of all the elements.

7 3 4 5

5 4 6 1   

6 9 4 2

3 2 7 5
asked by Suraj
edited by Ranjith

1 Answer

0 votes

Program

import java.util.Scanner;

public class TwoDArray {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[][] array = new int[4][4];
        System.out.print("Enter numbers: ");
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                array[i][j] = scanner.nextInt();
            }
        }
        int sum = 0;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                sum = sum + array[i][j];
            }
        }

        System.out.println("Sum = " + sum);
    }
}

Output

Enter numbers: 
7 3 4 5
5 4 6 1
6 9 4 2
3 2 7 5
Sum = 73

 

answered by Ranjith (2,683 points)
...