0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Print Odd Numbers from 1 to n

Use a for loop from 1 to n and print numbers where number % 2 !== 0, like: for(let i=1; i<=n; i++){ if(i % 2 !== 0) console.log(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 using the remainder operator %. If the number divided by 2 leaves a remainder of 1, it is odd, so print it. Continue 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 condition number % 2 !== 0.
4
If the number is odd, print it.
5
Repeat until the loop reaches n.
💻

Code

javascript
const n = 10;
for (let i = 1; i <= n; i++) {
  if (i % 2 !== 0) {
    console.log(i);
  }
}
Output
1 3 5 7 9
🔍

Dry Run

Let's trace the program with n = 5 through the code.

1

Initialize n and start loop

n = 5, i starts at 1

2

Check if i is odd

i = 1, 1 % 2 = 1 (odd), print 1

3

Increment i and repeat

i = 2, 2 % 2 = 0 (even), skip print

4

Continue checking

i = 3, 3 % 2 = 1 (odd), print 3

5

Continue checking

i = 4, 4 % 2 = 0 (even), skip print

6

Continue checking

i = 5, 5 % 2 = 1 (odd), print 5

ii % 2Print?
11Yes (print 1)
20No
31Yes (print 3)
40No
51Yes (print 5)
💡

Why This Works

Step 1: Loop from 1 to n

The for loop goes through every number starting at 1 up to n to check each number.

Step 2: Check odd numbers

Using i % 2 !== 0 tests if the number leaves a remainder when divided by 2, meaning it is odd.

Step 3: Print odd numbers

Only numbers passing the odd check are printed, so the output shows all odd numbers from 1 to n.

🔄

Alternative Approaches

Increment by 2 starting from 1
javascript
const n = 10;
for (let i = 1; i <= n; i += 2) {
  console.log(i);
}
This skips even numbers entirely, making the loop faster and simpler.
Using while loop
javascript
const n = 10;
let i = 1;
while (i <= n) {
  if (i % 2 !== 0) console.log(i);
  i++;
}
Uses a while loop instead of for, useful if you prefer while syntax.
Using Array and filter
javascript
const n = 10;
const odds = Array.from({length: n}, (_, i) => i + 1).filter(x => x % 2 !== 0);
odds.forEach(x => console.log(x));
Creates an array and filters odd numbers, more functional but uses extra memory.

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

Time Complexity

The loop runs from 1 to n once, so it takes linear time O(n). Checking oddness is constant time per iteration.

Space Complexity

The program uses a fixed amount of space regardless of n, so space complexity is O(1).

Which Approach is Fastest?

Incrementing by 2 is fastest because it skips even numbers, reducing iterations by half.

ApproachTimeSpaceBest For
Check each number with % 2O(n)O(1)Simple and clear
Increment by 2O(n/2)O(1)Faster, skips evens
Array filter methodO(n)O(n)Functional style, uses extra memory
💡
Use i += 2 to loop only through odd numbers for better performance.
⚠️
Beginners often forget to check the condition i % 2 !== 0 and print all numbers instead.