Java Program To Sum the elements of the Array
- July 28, 2020
- by

Java Program To Sum the elements of the Array
In this Java Program, we sum up all the elements of the array.
Array is a data structure consisting of collections of elements each identified by atleast one array index or key.
E.g. If you want to store 5 numbers in an array,
datatype arrayname [arraysize];
int a[5];
Step 1 : Create and Initialize array
Step 2 : Create variable sum and initialize it to zero
Step 3 : Apply advanced for loop where int number : array
Step 4 : Add sum to num and store it in the variable sum
Step 5 : Print the Result
Program :
public class SumArray
{
public static void main(String[] args)
{
int[] array = {10, 20, 30, 40, 50, 10};
int sum = 0;
//Advanced for loop
for(int num : array)
{
sum = sum + num;
}
System.out.println("Sum of array elements is: "+sum);
}
}
Output :
Sum of array elements is: 160
For the execution of the same do visit my YouTube Channel :

0 comments:
Post a Comment