C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Sum of elements in Double Dimensional Array Program - Java Programs

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

Sum = 73  
import java.util.*; class Sum{ public static void main(String args[]) { Scanner in = new Scanner (System.in); int a[][] = new int[4][4]; int sum = 0; System.out.println("Enter 16 integers:"); for(int i = 0; i < 4; i++){ for(int j = 0; j < 4; j++){ a[i][j] = in.nextInt(); sum += a[i][j]; } } System.out.println("Sum = " + sum); } }

Output

 
 OUTPUT : 
 
Enter 16 integers:
7
3
4
5
5
4
6
1
6
9
4
2
3
2
7
5
Sum = 73