Java Program to Display Odd Numbers

To display odd numbers in Java, you can start at 1 and increase the loop variable by 2, or iterate through every integer and print only the values that are not exactly divisible by 2. This tutorial demonstrates both approaches using while and for loops.

The examples first print positive odd numbers from 1 up to a given maximum n. For example, if n = 20, the output is 1, 3, 5, ..., 19.

How to Check Whether a Number Is Odd in Java

An integer is odd when it is not exactly divisible by 2. Java’s remainder operator % can be used to test this condition.

</>
Copy
number % 2 != 0

For example, 7 % 2 produces a nonzero remainder, so 7 is odd. In contrast, 8 % 2 is 0, so 8 is even.

For a loop that contains only positive integers, a condition such as i % 2 == 1 also identifies odd values. The more general test number % 2 != 0 is preferable when the input may also be negative, because Java can produce -1 as the remainder for a negative odd number.

Algorithms to Print Odd Numbers up to N in Java

Algorithm 1 – Start at 1 and Increment by 2

This approach directly generates the odd numbers. Start with odd = 1, print the current value while it is less than or equal to n, and add 2 after every iteration.

  1. Start.
  2. Take a value for n. This is the upper limit for the odd numbers printed to the console.
  3. Initialize variable odd with 1. The first positive odd number is 1.
  4. Check whether odd is less than or equal to n.
  5. If the condition is true, continue to step 6; otherwise, go to step 9.
  6. Print odd.
  7. Increment odd by 2 so that it contains the next odd number.
  8. Go to step 4.
  9. Stop.

Because this method moves directly from one odd number to the next, it does not need an additional if condition inside the loop.

Algorithm 2 – Test Each Number for an Odd Remainder

This alternative visits every integer from 1 through n and prints the current value only when it is odd.

  1. Start.
  2. Take a value for n. This is the upper limit for the numbers examined by the program.
  3. Initialize variable i with 1.
  4. Check whether i is less than or equal to n.
  5. If the condition is true, continue to step 6; otherwise, go to step 11.
  6. Check whether i leaves a nonzero remainder when divided by 2.
  7. If the condition is true, continue to step 8; otherwise, go to step 9.
  8. Print i.
  9. Increment i by 1.
  10. Go to step 4.
  11. Stop.

Both approaches produce the same sequence for positive values of n. The first generates odd numbers directly, while the second demonstrates how to test each number for oddness.

Display Odd Numbers up to N Using a Java While Loop

The following programs use n = 20. Therefore, they print every positive odd number from 1 through 20.

Java Program – Using Algorithm 1

</>
Copy
/**
 * Java Program - Display Odd Numbers
 */

public class DisplayOddNumbers {

	public static void main(String[] args) {
		//number
		int n = 20;
		
		//print all odd numbers <=n 
		int odd=1;
		while (odd<=n) {
			System.out.print(odd+"  ");
			odd += 2;
		}
	}
}

Here, odd starts at 1. After printing the current value, odd += 2 advances to the next odd number. When odd becomes greater than n, the while loop ends.

Java Program – Using Algorithm 2

</>
Copy
/**
 * Java Program - Display Odd Numbers
 */

public class DisplayOddNumbers {

	public static void main(String[] args) {
		//number
		int n = 20;
		
		//print all odd numbers <=n 
		int i=1;
		while (i<=n) {
			if(i%2==1) {
				System.out.print(i+"  ");
			}
			i++;
		}
	}
}

The second program increments i by 1, so every integer is examined. Since the loop contains only positive integers, i % 2 == 1 is true for the odd values.

Output

Run any of the above program, and we shall get all the odd numbers up to n, printed to the console.

1  3  5  7  9  11  13  15  17  19

Display Odd Numbers up to N Using a Java For Loop

A for loop can express the starting value, upper-limit condition, and increment in one statement. The same two odd-number approaches can therefore be written more compactly.

Java Program 2 – Using Algorithm 1

</>
Copy
/**
 * Java Program - Display Odd Numbers
 */

public class DisplayOddNumbers {

	public static void main(String[] args) {
		//number
		int n = 20;
		
		//print all odd numbers <=n 
		for (int i=1; i<=n; i+=2) {
			System.out.print(i+"  ");
		}
	}
}

In this program, the loop starts at 1 and uses i += 2. The values of i are therefore 1, 3, 5, 7, and so on until the upper limit is reached.

Java Program – Using Algorithm 2

</>
Copy
/**
 * Java Program - Display Odd Numbers
 */

public class DisplayOddNumbers {

	public static void main(String[] args) {
		//number
		int n = 20;
		
		//print all odd numbers <=n 
		for (int i=1; i<=n; i++) {
			if(i%2==1) {
				System.out.print(i+"  ");
			}
		}
	}
}

This version visits every integer from 1 to n and uses the remainder test to decide whether to print it.

Output

Run the above program, and we shall get all the odd numbers up to n, printed to the console.

1  3  5  7  9  11  13  15  17  19

Print Odd Numbers from 1 to a User-Entered N Using Scanner

If the upper limit should be entered at runtime instead of being fixed in the program, use Scanner to read n. The loop can then start at 1 and increase by 2.

</>
Copy
import java.util.Scanner;

public class DisplayOddNumbers {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter N: ");
        int n = scanner.nextInt();

        for (int i = 1; i <= n; i += 2) {
            System.out.print(i + " ");
        }

        scanner.close();
    }
}

If the user enters 20, the program prints the odd numbers from 1 through 20.

Enter N: 20
1 3 5 7 9 11 13 15 17 19

If n is less than 1, this particular loop prints no positive odd numbers because its starting value is 1.

Java Program to Print Odd Numbers from 1 to 100

When the required upper limit is specifically 100, set the loop condition to i <= 100. Starting at 1 and incrementing by 2 prints only odd values.

</>
Copy
public class OddNumbersTo100 {
    public static void main(String[] args) {
        for (int i = 1; i <= 100; i += 2) {
            System.out.print(i + " ");
        }
    }
}

The final value printed is 99 because 100 is even.

Print the First N Odd Numbers in Java

Printing odd numbers up to N is different from printing the first N odd numbers. The ith positive odd number can be calculated with 2 * i - 1. Therefore, a loop that runs exactly n times can generate the first n odd numbers.

</>
Copy
public class FirstNOddNumbers {
    public static void main(String[] args) {
        int n = 10;

        for (int i = 1; i <= n; i++) {
            System.out.print((2 * i - 1) + " ");
        }
    }
}

For n = 10, the program prints the first 10 positive odd numbers.

1 3 5 7 9 11 13 15 17 19

Choosing the Right Java Loop for Odd Numbers

  • Use i += 2 when the goal is simply to generate positive odd numbers efficiently from 1 up to an upper limit.
  • Use number % 2 != 0 when you need to check whether arbitrary integers are odd, including negative values.
  • Use 2 * i - 1 when the requirement is to print the first n positive odd numbers rather than all odd numbers below a maximum value.

Java Odd Number Printing: Key Points

In this Java Tutorial, we learned how to print odd numbers up to a given number using while and for loops. We also used the remainder operator to identify odd integers, accepted the upper limit using Scanner, printed odd numbers from 1 to 100, and distinguished printing odd numbers up to n from printing the first n odd numbers.