Java Program to find Sum of Odd Numbers in an Array

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

Java Program to find Sum of 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 odd numbers within this array using For Loop.

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

public class SumOfOdds1 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Size, i, 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)
			{
				OddSum = OddSum + a[i]; 
			}
		}		
		System.out.println("\n The Sum of Odd Numbers in this Array = " + OddSum);
	}
}
Java Program to find Sum of Odd Numbers in an Array 1

First, we used for loop to iterate each element. Within the loop, we used the Java If statement to check if the number divisible by 2 is not equal to 0 or not.

for(i = 0; i < Size; i++)
{
	if(a[i] % 2 != 0)
	{
		OddSum = OddSum + a[i]; 
	}
}

User inserted Array values are a[5] = {10, 25, 35, 60, 77}}, OddSum = 0

First Iteration: for (i = 0; 0 < 5; 0++)
The value of i will be 0, and the condition (i < 5) is True. So, Java compiler will enter into the If statements inside the For Loop.

if(a[i] % 2 != 0) => if(a[0] % 2 != 0)
if(10 % 2 != 0) – Condition is False.

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 True.
OddSum = OddSum + a[i]
OddSum = 0 + 25 = 25

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

if(a[2] % 2 != 0)
if(35 % 2 != 0) – Condition is True.
OddSum = 25 + 35 = 60

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

Java Program to find Sum of Odd Numbers 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 odd numbers in an array.

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

public class SumOfOdds2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Size, i = 0, j = 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)
			{
				OddSum = OddSum + a[j]; 
			}
			j++;
		}		
		System.out.println("\n The Sum of Odd Numbers in this Array = " + OddSum);
	}
}

Java Sum of Odd Array Numbers using a While Loop output

 Please Enter Number of elements in an array : 10
 Please Enter 10 elements of an Array  : 15 25 98 75 62 14 12 13 22 77

 The Sum of Odd Numbers in this Array = 205

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

This Java program is the same as the first example. However, we separated the logic to calculate the sum of odd numbers using Method.

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

public class SumOfOdds3 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Size, i, 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();
		}   
		OddSum = SumOfOddsinArray(a, Size);
		System.out.println("\n The Sum of Odd Numbers in this Array = " + OddSum);
	}
	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;
	}
}

Java Sum of Odd Numbers in an Array using functions output

 Please Enter Number of elements in an array : 8
 Please Enter 8 elements of an Array  : 5 19 35 146 89 17 28 105

 The Sum of Odd Numbers in this Array = 270