Answer:
public class sum{
public static void main(String []args){
int sum = 0;
int[] a = {9,2,8,4,0,6};
for ( int i = 0; i < a.length; i++ )
{
sum = sum + a[i];
}
System.out.println("The sum is: "+sum );
}
}
Step-by-step explanation:
First create the class in java programming.
Then create the main function and declare the variable and array.
To calculate the sum of all element in array, first we have to traverse the array pick element one by one and then add,
so, we have to use loop for traversing and add with sum which inialize with zero.
suppose array element is 1,2,3,4
sum = sum +a[1] means sum = 0 +1=1
the sum = 1+2=3, sum = 3+4=7.
and then finally print the result store in sum.