In this article, we will show you, How to write a Java Program to find Sum of Even and Odd Numbers in an Array using For Loop, While Loop, and Functions with example.
Java Program to find Sum of Even and Odd Numbers in an Array using For Loop
This program allows the user to enter the size, and the Array elements. Next, it will find the sum of even numbers, and sum of odd numbers within this array using For Loop.
// Java Program to find Sum of Even and Odd Numbers in an Array using For Loop import java.util.Scanner; public class SumOfEvenOdds1 { private static Scanner sc; public static void main(String[] args) { int Size, i, EvenSum = 0, OddSum = 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]; } else { OddSum = OddSum + a[i]; } } System.out.println("\n The Sum of Even Numbers in this Array = " + EvenSum); System.out.println(" The Sum of Odd Numbers in this Array = " + OddSum); } }
OUTPUT
ANALYSIS
Any number that is divisible by 2 is even number. If statement will check whether the remainder of the current array element divided by 2 is exactly equal to 0 or not.
- If the condition is True then it is an Even number, and the compiler will add the value to EvenSum.
- If the condition is False then it is an Odd number. Compiler will add value to OddSum.
for(i = 0; i < Size; i++) { if(a[i] % 2 == 0) { EvenSum = EvenSum + a[i]; } else { OddSum = OddSum + a[i]; } }
User inserted values are a[5] = {10, 15, 20, 55, 95}}
First Iteration: for (i = 0; 0 < 5; 0++)
The value of i will be 0 and the condition (i < 5) is True.
if(a[i] % 2 == 0) => if(a[0] % 2 == 0)
if(10 % 2 == 0) – Condition is True.
EvenSum = EvenSum + a[0]
EvenSum = 0 + 10 = 10
Second Iteration: for (i = 1; 1 < 5; 1++)
The condition (i < 5) is True.
if(a[1] % 2 == 0)
if(15 % 2 == 0) – Condition is False.
OddSum = OddSum + a[1]
OddSum = 0 + 15 = 15
Third Iteration: for (i = 2; 2 < 5; 2++)
The value of i will be 0 and the condition (i < 5) is True.
if(a[2] % 2 == 0)
if(20 % 2 == 0) – Condition is True.
EvenSum = EvenSum + a[2]
EvenSum = 10 + 20 = 30
Do the same for remaining iterations until the condition (i < 5) fails.
Java Program to find Sum of Even and Odd Numbers in an Array using While Loop
This program is same as above but this time we used While Loop to calculate the sum of even and odd numbers an array.
// Java Program to find Sum of Even and Odd Numbers in an Array using While Loop import java.util.Scanner; public class SumOfEvenOdds2 { private static Scanner sc; public static void main(String[] args) { int Size, i = 0, j = 0, EvenSum = 0, OddSum = 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]; } else { OddSum = OddSum + a[j]; } j++; } System.out.println("\n The Sum of Even Numbers in this Array = " + EvenSum); System.out.println(" The Sum of Odd Numbers in this Array = " + OddSum); } }
OUTPUT
Java Program to find Sum of Even and Odd Numbers in an Array using Methods
This program is same as first example. But this time we created separate method to calculate sum of even numbers, and another method to calculate sum of odd numbers.
// Java Program to find Sum of Even and Odd Numbers in an Array using Functions import java.util.Scanner; public class SumOfEvenOdds3 { private static Scanner sc; public static void main(String[] args) { int Size, i, EvenSum = 0, OddSum = 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); OddSum = SumOfOddsinArray(a, Size); System.out.println("\n The Sum of Even Numbers in this Array = " + EvenSum); System.out.println(" The Sum of Odd Numbers in this Array = " + OddSum); } 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; } public static int SumOfOddsinArray(int[] a, int Size) { int i, OddSum = 0; for(i = 0; i < Size; i++) { if(a[i] % 2 != 0) { OddSum = OddSum + a[i]; } } return OddSum; } }
OUTPUT
Thank you for visiting our Blog