Java Program to find Sum of Even and Odd Numbers in an Array

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 Java program allows the user to enter the size and 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);
	}
}
Java Program to find Sum of Even and Odd Numbers in an Array 1

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 Java compiler will add the value to EvenSum.
  • If the condition is False, then it is an Odd number. The Java 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 Array 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 For Loop 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 Java program to calculate sum of even and odd in an array is the same as above. But this time, we used Java 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);
	}
}
 Please Enter Number of elements in an array : 8
 Please Enter 8 elements of an Array  : 15 25 20 40 60 89 97 200

 The Sum of Even Numbers in this Array = 320
 The Sum of Odd Numbers in this Array = 226

Java Program to find Sum of Even and Odd Numbers in an Array using Methods

This program is the same as the first example. But this time, we created a separate method to calculate the sum of even numbers and another method to calculate the sum of odd numbers.

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;
	}
}
 Please Enter Number of elements in an array : 10
 Please Enter 10 elements of an Array  : 14 24 59 89 100 209 77 16 125 196

 The Sum of Even Numbers in this Array = 350
 The Sum of Odd  Numbers in this Array = 559