Java Program to find Sum of Elements in an Array

Write a Java Program to find Sum of Elements in an Array using For Loop, While Loop, and Functions with example.

Java Program to find Sum of Elements in an Array using For Loop

This Java program allows the user to enter the size and Array elements. Next, it will find the sum of all the existing elements within this array using For Loop.

// Java Program to find Sum of Elements in an Array using For Loop
import java.util.Scanner;

public class SumOfAllElements1 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Size, i, Sum = 0;
		sc = new Scanner(System.in);
	 
		System.out.print(" Please Enter Number of elements in an array : ");
		Size = sc.nextInt();	
		
		int [] a = new int[Size];
		
		System.out.print(" Please Enter " + Size + " elements of an Array  : ");
		for (i = 0; i < Size; i++)
		{
			a[i] = sc.nextInt();
		}   

		for(i = 0; i < Size; i++)
		{
			Sum = Sum + a[i]; 
		}		
		System.out.println("\n The Sum of All Elements in this Array = " + Sum);
	}
}
Java Program to find Sum of Elements in an Array 1

First, we used for loop to iterate each element. Within the For Loop, we are adding every element (value at the position) to Sum.

for(i = 0; i < Size; i++)
{
	Sum = Sum + a[i]; 
}

User inserted values are a[5] = {10, 25, 30, 65, 92}} and Sum = 0

First Iteration: for (i = 0; 0 < 5; 0++)
The value of i will be 0, and the condition (i < 5) is True. So, it will start executing the Java statements inside the loop until the condition fails.

Sum = Sum + a[0]
Sum = 0 + 10 = 10

Second Iteration: for (i = 1; 1 < 5; 1++)
The condition (1 < 5) is True.

Sum = 10 + 25 = 35

Second Iteration: for (i = 2; 2 < 5; 2++)
The condition (2 < 5) is True.

Sum = 35 + 30 = 65

Please do the same for the remaining iterations until the condition (i < 5) fails.

Java Program to find Sum of Elements in an Array using While Loop

This Java program is the same as above. But this time, we used While Loop to calculate the sum of all elements in an Array.

// Java Program to find Sum of Elements in an Array using While Loop
import java.util.Scanner;

public class SumOfAllElements2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Size, i = 0, j = 0, Sum = 0;
		sc = new Scanner(System.in);
	 
		System.out.print(" Please Enter Number of elements in an array : ");
		Size = sc.nextInt();	
		
		int [] a = new int[Size];
		
		System.out.print(" Please Enter " + Size + " elements of an Array  : ");
		while(i < Size)
		{
			a[i] = sc.nextInt();
			i++;
		}   

		while(j < Size)
		{
			Sum = Sum + a[j];
			j++;
		}		
		System.out.println("\n The Sum of All Elements in this Array = " + Sum);
	}
}

Java sum of array items using a while loop output

 Please Enter Number of elements in an array : 7
 Please Enter 7 elements of an Array  : 10 20 33 55 77 88 99

 The Sum of All Elements in this Array = 382

Program to find Sum of Elements in an Array using Methods

This Java program is the same as the first example. Here, we separated the logic to find the sum of array elements using Method.

// Java Program to find Sum of Elements in an Array using Functions
import java.util.Scanner;

public class SumOfAllElements3 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Size, i, Sum = 0;
		sc = new Scanner(System.in);
	 
		System.out.print(" Please Enter Number of elements in an array : ");
		Size = sc.nextInt();	
		
		int [] a = new int[Size];
		
		System.out.print(" Please Enter " + Size + " elements of an Array  : ");
		for (i = 0; i < Size; i++)
		{
			a[i] = sc.nextInt();
		}   

		Sum = SumOfArray(a, Size);
		System.out.println("\n The Sum of All Elements in this Array = " + Sum);
	}
	public static int SumOfArray(int[] a, int Size)
	{
		int i, Sum = 0;
		
		for(i = 0; i < Size; i++)
		{
			Sum = Sum + a[i]; 
		}	
		return Sum;
	}
}

Java sum of array items using for loop and functions output

 Please Enter Number of elements in an array : 10
 Please Enter 10 elements of an Array  : 5 10 15 20 25 30 35 40 125 895

 The Sum of All Elements in this Array = 1200