198k views
1 vote
The code calculates and prints the sum of all the elements in the array a.

Int sum = 0;

For ( int I = 0; I < a.length; i++ )

{

//Your code goes here.

}

System .out.println( %u201Csum is %u201C + sum );

User Kawty
by
5.7k points

1 Answer

2 votes

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.

User TechFree
by
5.0k points