In this tutorial, we will learn how to write a Java program to print odd numbers from 1 to N, where N is a number entered by the user.
For example, if the user enters 20, the program will display all odd numbers between 1 and 20, such as 1, 3, 5, 7, 9, 11, 13, 15, 17, and 19.
In Java, we can print odd numbers using different looping statements, such as for, while, and do-while loops. We can also write a program to generate odd numbers without using an if statement.
Let’s understand the basic logic of odd numbers before exploring these approaches.
What Is an Odd Number in Java?
An odd number is an integer that cannot be divided evenly by 2. In other words, when an odd number is divided by 2, it leaves a nonzero remainder.
Some examples of odd numbers are 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, …
In Java, we can identify an odd number using the remainder (modulus) operator (%).
Condition to Check an Odd Number in Java
The following condition checks whether an integer number (byte, short, int, or long) is odd:
number % 2 != 0
Here, the % remainder operator returns the remainder after dividing number by 2. If the remainder is not equal to 0, the number is odd.
For example:
- 7 % 2 evaluates to 1, so 7 is odd.
- 10 % 2 evaluates to 0, so 10 is even.
- 15 % 2 evaluates to 1, so 15 is odd.
You can use the condition number % 2 != 0 directly inside an if statement (or return it directly from a boolean method) to test for odd numbers in Java.
Java Program to Print Odd Numbers from 1 to N Using for Loop
The for loop in Java is a simple way to display all odd numbers within a given range.
In this approach, we initialize a loop variable i with 1 and execute the loop until its value exceeds N. Inside the loop, we check whether the current number is odd using the condition i % 2 != 0 inside the if statement.
If the condition is true, the program prints the number. Otherwise, it moves to the next iteration.
Let’s write a complete Java program to print odd numbers from 1 to N using a for loop. Here, we will take the value of N from the user’s keyboard using the Scanner class.
Complete Java Program
// Import Scanner to read input from standard input (System.in)
import java.util.Scanner;
/**
* Prints all odd integers from 1 up to a user-specified upper limit N.
*/
public class PrintOddNumbers {
public static void main(String[] args) {
// Create a Scanner object to read user input from the console
Scanner sc = new Scanner(System.in);
// Prompt the user and read the upper limit N
System.out.print("Enter the value of N: ");
int n = sc.nextInt();
// Ensure N is at least 1 so there is a valid range [1, N] to inspect
if (n >= 1) {
System.out.println("Odd numbers from 1 to " + n + " are:");
// Loop through every integer from 1 to N (inclusive)
for (int i = 1; i <= n; i++) {
// Remainder != 0 means i is not divisible by 2, so i is odd
if (i % 2 != 0) {
System.out.print(i + " "); // Print the odd number followed by a space
}
}
// Move to a new line after printing all odd numbers on one line
System.out.println();
} else {
// Handle zero or negative inputs
System.out.println("Please enter a positive integer.");
}
// Close the Scanner to release the underlying input stream resource
sc.close();
}
}Output:
Enter the value of N: 25 Odd numbers from 1 to 25 are: 1 3 5 7 9 11 13 15 17 19 21 23 25
Explanation of the Java Program
Let’s understand how the above Java program works step by step.
- Import Scanner class: We import the Scanner class from the java.util package to accept input from the keyboard.
- Create Scanner object: We create an object of the Scanner class using
new Scanner(System.in). - Read the upper limit: The
nextInt()method reads an integer value entered by the user and stores it in the variable named n. - Validate the input: The condition
n >= 1ensures that the entered value is a positive integer. - Initialize the loop: We initialize the variable i with 1 because 1 is the first positive odd number.
- Check the loop condition: The expression
i <= nensures that the loop continues as long as the current number does not exceed N. - Check for an odd number: The condition
i % 2 != 0checks whether the remainder after dividing the current number by 2 is nonzero. - Print the odd number: If the condition evaluates to true,
System.out.print(i + " ")displays the current odd number followed by a space. - Increment the counter: The expression
i++increases the value of i by 1 after every iteration. - Terminate the loop: When the value of i becomes greater than the specified limit N, the loop condition becomes false and the execution of the program stops.
As a result, the program displays all odd numbers from 1 up to the specified limit.
Java Program to Print Odd Numbers Without Using if Statement
We can also print odd numbers in Java without using an if statement. Instead of checking every integer to determine whether it is odd, we can obtain odd numbers directly by increasing the loop variable by 2.
Since 1 is the first positive odd number, we initialize the counter variable i with 1. After printing each number, we increase its value by 2 using the expression i += 2. This approach skips all even numbers automatically.
Complete Java Program
import java.util.Scanner;
/**
* Prints all odd integers from 1 up to N without using an if condition inside the loop.
*/
public class PrintOddWithoutIf {
public static void main(String[] args) {
// Create a Scanner object to read user input from the console
Scanner sc = new Scanner(System.in);
// Prompt the user and read the upper limit N
System.out.print("Enter the value of N: ");
int n = sc.nextInt();
// Validate that N is at least 1 so the range [1, N] contains numbers
if (n >= 1) {
System.out.println("Odd numbers from 1 to " + n + " are:");
// Start at the first odd number (1) and step by 2 so every value of i is odd
for (int i = 1; i <= n; i += 2) {
System.out.print(i + " "); // Print the current odd number followed by a space
}
// Print a trailing newline after all odd numbers are displayed
System.out.println();
} else {
// Inform the user when N is zero or negative
System.out.println("Please enter a positive integer.");
}
// Close the Scanner to release the input stream resource
sc.close();
}
}Output:
Enter the value of N: 25 Odd numbers from 1 to 25 are: 1 3 5 7 9 11 13 15 17 19 21 23 25
Explanation of the Java Program
Let’s understand the important statements used in this program.
- Initialize the loop variable: We initialize the loop variable i with 1 because it is the first positive odd number.
- Check the loop condition: The expression
i <= nensures that the loop executes only while the current number is within the specified range. - Print the number: The statement
System.out.print(i + " ")prints the current odd number on the console. - Increase the value by 2: The expression
i += 2increases the value of i by 2 after each iteration. It is equivalent toi = i + 2.
Since we increase the loop variable by 2, every generated value is odd. Therefore, there is no need to check the number using the modulus operator.
Java Program to Print Odd Numbers Using while Loop
A while loop in Java is another way to display odd numbers from 1 to N. In this approach, we initialize a loop variable i with 1 before entering the loop. The while loop checks whether the value of i is less than or equal to N.
If the condition is true, the program prints the current value and increases it by 2. The loop continues until the value of i becomes greater than N. Let’s write a Java program to print odd numbers using while loop.
Complete Java Program
import java.util.Scanner;
/**
* Prints all odd integers from 1 up to N using a while loop.
*/
public class PrintOddWhile {
public static void main(String[] args) {
// Create a Scanner object to read user input from the console
Scanner sc = new Scanner(System.in);
// Prompt the user and read the upper limit N
System.out.print("Enter the value of N: ");
int n = sc.nextInt();
// Validate that N is at least 1 so there is a valid range [1, N]
if (n >= 1) {
System.out.println("Odd numbers from 1 to " + n + " are:");
// Start at the first odd number
int i = 1;
// Repeat while the current odd number does not exceed N
while (i <= n) {
// Print the current odd number followed by a space
System.out.print(i + " ");
// Advance by 2 to get the next odd number
i += 2;
}
// Print a trailing newline after all odd numbers are displayed
System.out.println();
} else {
// Inform the user when N is zero or negative
System.out.println("Please enter a positive integer.");
}
// Close the Scanner to release the underlying input stream resource
sc.close();
}
}Output:
Enter the value of N: 10 Odd numbers from 1 to 10 are: 1 3 5 7 9
Coding Challenge: Print Odd Numbers Using Do-While Loop in Java
Your Task:
Write a Java program to print all odd numbers from 1 to N using a do-while loop.
Follow these instructions:
- Ask the user to enter an integer N.
- Initialize a variable i with 1 because 1 is the first positive odd number.
- Use a do-while loop to print odd numbers from 1 to N.
- Increase the value of i by 2 after each iteration.
- Since a do-while loop executes at least once, make sure the loop executes only when N is greater than or equal to 1.
- Display an appropriate message if the entered number is less than 1.
Hint: Initialize i = 1, print its value inside the do block, and use i += 2 to generate the next odd number. Check the condition i <= n in the while statement.


