0
0
JavaProgramBeginner · 2 min read

Java Program to Print Odd Numbers from 1 to n

Use a for loop from 1 to n and print numbers where i % 2 != 0, like for (int i = 1; i <= n; i++) { if (i % 2 != 0) System.out.println(i); }.
📋

Examples

Input5
Output1 3 5
Input10
Output1 3 5 7 9
Input1
Output1
🧠

How to Think About It

To print odd numbers from 1 to n, start counting from 1 and check each number if it is odd by seeing if it leaves a remainder when divided by 2. If yes, print it. Repeat this until you reach n.
📐

Algorithm

1
Get the input number n.
2
Start a loop from 1 to n.
3
For each number, check if it is odd by using the remainder operator.
4
If the number is odd, print it.
5
Continue until the loop reaches n.
💻

Code

java
public class OddNumbers {
    public static void main(String[] args) {
        int n = 10; // You can change this value
        for (int i = 1; i <= n; i++) {
            if (i % 2 != 0) {
                System.out.println(i);
            }
        }
    }
}
Output
1 3 5 7 9
🔍

Dry Run

Let's trace the program with n = 5 to see how it prints odd numbers.

1

Start loop with i = 1

Check if 1 % 2 != 0 (true), print 1

2

Next i = 2

Check if 2 % 2 != 0 (false), do not print

3

Next i = 3

Check if 3 % 2 != 0 (true), print 3

4

Next i = 4

Check if 4 % 2 != 0 (false), do not print

5

Next i = 5

Check if 5 % 2 != 0 (true), print 5

ii % 2 != 0Printed
1true1
2false
3true3
4false
5true5
💡

Why This Works

Step 1: Loop from 1 to n

The program uses a for loop to go through every number starting at 1 up to n.

Step 2: Check if number is odd

It uses the condition i % 2 != 0 to find odd numbers because odd numbers leave a remainder of 1 when divided by 2.

Step 3: Print odd numbers

When the condition is true, the program prints the number, showing only odd numbers from 1 to n.

🔄

Alternative Approaches

Increment by 2 starting from 1
java
public class OddNumbers {
    public static void main(String[] args) {
        int n = 10;
        for (int i = 1; i <= n; i += 2) {
            System.out.println(i);
        }
    }
}
This method skips even numbers by increasing the counter by 2, making it more efficient and simpler.
Using while loop
java
public class OddNumbers {
    public static void main(String[] args) {
        int n = 10;
        int i = 1;
        while (i <= n) {
            if (i % 2 != 0) {
                System.out.println(i);
            }
            i++;
        }
    }
}
This uses a while loop instead of for, which is useful if you want more control over the loop variable.

Complexity: O(n) time, O(1) space

Time Complexity

The program runs a loop from 1 to n, checking each number once, so it takes linear time O(n).

Space Complexity

It uses only a few variables and prints directly, so space used is constant O(1).

Which Approach is Fastest?

Incrementing by 2 is faster because it skips even numbers, reducing the number of iterations roughly by half.

ApproachTimeSpaceBest For
Check each number with % operatorO(n)O(1)Simple and clear logic
Increment by 2 starting from 1O(n/2)O(1)More efficient, fewer iterations
While loop with conditionO(n)O(1)When loop control needs flexibility
💡
Use i += 2 starting from 1 to print odd numbers more efficiently.
⚠️
Beginners often forget to check the condition i % 2 != 0 and print all numbers instead of only odd ones.