pTutorial

New way to learn Programming

Java two dimensional array

Following Java program calculate the sum of the two dimensional array. We use for loop to calculate the sum of the two dimensional array.

Add two dimensional array

import java.util.Scanner;
public class SumOfTwoDiementionalArrayExample{ 
public static void main(String... z)
{
   int a[][];
   int n, sum=0;
    Scanner uk = new Scanner(System.in);
    System.out.println("Enter the number of element");
    n = uk.nextInt();
    a = new int[n][n];
    
    for(int i = 0; i < a.length; i++){
        for(int j = 0; j<a[i].length; j++){
        a[i][j]=uk.nextInt();
        }
    }
    for(int i=0;i<a.length;i++){
        for(int j=0;j<a[i].length;j++){
            sum = sum+ a[i][j];
        }
    }
    System.out.println(sum);
}
}

Output

Enter the number of element
3
1 2 3 
1 2 2
1 1 1 
sum=14

See Also


QUOTES OF THE DAY

Saying is one thing, and doing is another.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER