Java Program to Calculate Sum of Odd Numbers

Write a Java Program to Calculate Sum of Odd Numbers using For Loop, and While Loop with example. The number that is not divisible by 2 is odd.

Java Program to Calculate Sum of Odd Numbers using For Loop

This program allows the user to enter the maximum limit value. Next, this Java program calculates the sum of odd numbers from 1 to maximum limit value using For Loop and If statement.

TIP: We already explained the logic to check the number is Even or Not in Java Odd or Even Program article in Java. I suggest you refer to the same.

// Java Program to Calculate Sum of Odd Numbers using for loop
import java.util.Scanner;

public class SumofOdd1 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int number, i, oddSum = 0;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Number : ");
		number = sc.nextInt();	
		
		for(i = 1; i <= number; i++)
		{
			if(i % 2 != 0)
			{
				oddSum = oddSum + i; 
			}
		}
		System.out.println("\n The Sum of Odd Numbers upto " + number + "  =  " + oddSum);
	}
}
Java Program to Calculate Sum of Odd Numbers 1

The For loop is to iterate from 1 to maximum value (Here, number = 5). Next, we used the If condition to check whether the remainder of the number divided by 2 is not equal to 0.

User entered value for this Java Program to find Sum of Odd Numbers : number  = 5

For Loop First Iteration: for(i = 1; i <= 5; i++)
if(i % 2 != 0) => if(1 % 2 != 0) – Condition is True.

oddSum = oddSum + i
oddSum = 0 + 1 = 1

Second Iteration: for(i = 2; 2 <= 5; 2++)
if(2 % 2 != 0) – Condition is False.

Third Iteration: for(i = 3; 3 <= 5; 3++)
if(3 % 2 != 0) – Condition is True.

oddSum = oddSum + i
oddSum = 1 + 3 = 4

Fourth Iteration: for(i = 4; 4 <= 5; 4++)
if(4 % 2 != 0) – False.

Fifth Iteration: for(i = 5; 5 <= 5; 5++)
if(5 % 2 != 0) – Condition is True.

oddSum = oddSum + i
oddSum = 4 + 5 = 9

Sixth Iteration: for(i = 6; 6 <= 5; 6++)
Condition (6 <= 5) is False. So, the Java compiler exits from the For Loop

Java Program to Calculate Sum of Odd Numbers Example 2

In this Java program to find the sum of odd numbers, we used for loop without the If statement. If you observe the Java code, we started i from 1 and incremented by 2 (not 1). It means for the first iteration i = 1, and for the second iteration i = 3 (not 2) so on.

// Java Program to Calculate Sum of Odd Numbers using for loop
import java.util.Scanner;

public class SumofOdd2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int number, i, oddSum = 0;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Number : ");
		number = sc.nextInt();	
		
		for(i = 1; i <= number; i = i + 2)
		{
			oddSum = oddSum + i; 
		}
		System.out.println("\n The Sum of Odd Numbers upto " + number + "  =  " + oddSum);
	}
}

Java sum of odd numbers using for loop output

 Please Enter any Number : 30

 The Sum of Odd Numbers upto 30  =  225

Java Program to Calculate Sum of Odd Numbers using While Loop

This Java program to find the sum of odd is the same as the second example, but we are using the While Loop.

// Java Program to Calculate Sum of Odd Numbers using While loop
import java.util.Scanner;

public class SumofOdd3 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int number, i = 1, oddSum = 0;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Number : ");
		number = sc.nextInt();	
		
		while(i <= number)
		{
			oddSum = oddSum + i; 
			i = i + 2;
		}
		System.out.println("\n The Sum of Odd Numbers upto " + number + "  =  " + oddSum);
	}
}
 Please Enter any Number : 40

 The Sum of Odd Numbers upto 40  =  400

Java Program to Calculate Sum of Odd Numbers using Method

This program is the same as the first example, but we separated the sum of odd logic and placed it in a separate method.

// Java Program to Calculate Sum of Odd Numbers
import java.util.Scanner;

public class SumofOdd4 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int number, oddSum = 0;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Number : ");
		number = sc.nextInt();	
		
		oddSum = sumOfOdd(number);
		
		System.out.println("\n The Sum of Odd Numbers upto " + number + "  =  " + oddSum);
	}
		public static int sumOfOdd(int num)
		{
			int i, sum = 0;
			for(i = 1; i <= num; i++)
			{
				if(i % 2 != 0)
				{
					sum = sum + i; 
				}
			}
			return sum;
		}
}
 Please Enter any Number : 100

 The Sum of Odd Numbers upto 100  =  2500

Java Program to find Sum of Odd Numbers between a Given Range

This Java program allows the user to enter Minimum and maximum value. Next, the Java program finds the sum of odd numbers between Minimum value and maximum value.

// Java Program to find Sum of Odd Numbers between Maximum and Minimum
import java.util.Scanner;

public class SumofOdd5 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int minimum, maximum, oddSum = 0;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter the Minimum value : ");
		minimum = sc.nextInt();	
		
		System.out.print(" Please Enter the Maximum value : ");
		maximum = sc.nextInt();	
		
		oddSum = sumOfOdd(minimum, maximum);	
		System.out.println("\n The Sum of Odd Numbers from " + minimum + " to " + maximum + "  =  " + oddSum);
	}
	
	public static int sumOfOdd(int minimum, int maximum)
	{
		int i, sum = 0;
		if(minimum % 2 != 0)
		{
			minimum++;
		}
		for(i = minimum; i <= maximum; i++)
		{
			if(i % 2 != 0)
			{
				sum = sum + i;  
			}
		}
		return sum;
	}
}

Java sum of odd numbers between 1 and 100 output

 Please Enter the Minimum value : 20
 Please Enter the Maximum value : 200

 The Sum of Odd Numbers from 20 to 200  =  9900