Write a Java Program to find the Sum of Elements in an Array using For Loop, While Loop, and Functions with an example.
Java Program to find Sum of Elements in an Array using For Loop
This program allows the user to enter the size and Array of elements. Next, it will find the sum of all the existing elements within this 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); } }

First, we used for loop to iterate each element. Within the For Loop, we are adding every element (value at the position) to Sum.
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.
import java.util.Scanner; public class Example2 { private static Scanner sc; public static void main(String[] args) { int Size, i = 0, j = 0, tot = 0; sc = new Scanner(System.in); System.out.print(" Please Enter Number of elements : "); Size = sc.nextInt(); int [] a = new int[Size]; System.out.print(" Please Enter " + Size + " elements : "); while(i < Size) { a[i] = sc.nextInt(); i++; } while(j < Size) { tot = tot + a[j]; j++; } System.out.println("\n The Sum of All Elements in this Array = " + tot); } }
Please Enter Number of elements : 7
Please Enter 7 elements : 10 20 33 55 77 88 99
The Sum of All Elements in this Array = 382
Java program to find the Sum of Elements in an Array using functions
This program is the same as the first example. Here, we separated the logic to find the sum of array elements using Method.
import java.util.Scanner; public class Example3 { private static Scanner sc; public static void main(String[] args) { int Size, i, Sm = 0; sc = new Scanner(System.in); System.out.print(" Please Enter Number of elements : "); Size = sc.nextInt(); int [] a = new int[Size]; System.out.print(" Please Enter " + Size + " elements : "); for (i = 0; i < Size; i++) { a[i] = sc.nextInt(); } Sm = SmOfArr(a, Size); System.out.println("\n The Sum of All Elements in this Array = " + Sm); } public static int SmOfArr(int[] a, int Size) { int i, Sm = 0; for(i = 0; i < Size; i++) { Sm = Sm + a[i]; } return Sm; } }
Please Enter Number of elements : 10
Please Enter 10 elements : 5 10 15 20 25 30 35 40 125 895
The Sum of All Elements in this Array = 1200