How to write a Java Program to find Sum of Even Numbers in an Array using For Loop, While Loop, and Functions with example.
Java Program to find Sum of Even Numbers 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 even numbers (or elements) within this array using For Loop.
// Java Program to find Sum of Even Numbers in an Array using For Loop import java.util.Scanner; public class SumOfEvens1 { private static Scanner sc; public static void main(String[] args) { int Size, i, EvenSum = 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++) { if(a[i] % 2 == 0) { EvenSum = EvenSum + a[i]; } } System.out.println("\n The Sum of Even Numbers in this Array = " + EvenSum); } }
First, we used Java For Loop to iterate each element. Within the Loop, we used the Java If statement to check if the number is divisible by 2
for(i = 0; i < Size; i++) { if(a[i] % 2 == 0) { EvenSum = EvenSum + a[i]; } }
User inserted Array values are a[5] = {10, 25, 19, 20, 50}} and EvenSum = 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.
if(a[i] % 2 == 0) => if(a[0] % 2 == 0)
if(10 % 2 == 0) – Condition is True.
EvenSum = 0 + 10 = 10
Second Iteration: for (i = 1; 1 < 5; 1++)
The condition (i < 5) is True.
if(a[1] % 2 == 0)
if(25 % 2 == 0) – Condition is False
Do the same for remaining iterations until the condition (i < 5) fails.
Java Program to find Sum of Even Numbers in an Array using While Loop
This program is the same as above. But this time, we used Java While Loop to calculate the sum of even numbers in an array.
// Java Program to find Sum of Even Numbers in an Array using While Loop import java.util.Scanner; public class SumOfEvens2 { private static Scanner sc; public static void main(String[] args) { int Size, i = 0, j = 0, EvenSum = 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) { if(a[j] % 2 == 0) { EvenSum = EvenSum + a[j]; } j++; } System.out.println("\n The Sum of Even Numbers in this Array = " + EvenSum); } }
Java sum of even items in an array using a while loop output
Please Enter Number of elements in an array : 8
Please Enter 8 elements of an Array : 10 20 15 35 79 88 98 2
The Sum of Even Numbers in this Array = 218
Java Program to Calculate Sum of Even Numbers in an Array using Methods
This Java program is the same as first example. Here, we separated the logic to calculate the sum of even numbers using Method.
// Java Program to find Sum of Even Numbers in an Array using Functions import java.util.Scanner; public class SumOfEvens3 { private static Scanner sc; public static void main(String[] args) { int Size, i, EvenSum = 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(); } EvenSum = SumOfEvensinArray(a, Size); System.out.println("\n The Sum of Even Numbers in this Array = " + EvenSum); } public static int SumOfEvensinArray(int[] a, int Size) { int i, EvenSum = 0; for(i = 0; i < Size; i++) { if(a[i] % 2 == 0) { EvenSum = EvenSum + a[i]; } } return EvenSum; } }
Java even array items sum using functions output
Please Enter Number of elements in an array : 10
Please Enter 10 elements of an Array : 5 25 20 30 19 60 50 14 17 89
The Sum of Even Numbers in this Array = 174