Introduction
While working through basic programming exercises, I encountered a problem that initially looked straightforward but turned out to be a good exercise in logical decomposition:
Print the sequence
1 3 5 7 9 2 4 6 8 10
using a single loop.
At first, the problem seems trivial. However, the constraint of using a single loop introduces an important requirement: we cannot separately iterate over odd and even numbers using two loops. Instead, we need to identify a unified logic that produces both sequences in a controlled manner.
This is where the actual thinking begins.
Understanding the Pattern
Before writing any code, I focused on understanding the structure of the output.
If we rewrite the sequence visually:
1 3 5 7 9 | 2 4 6 8 10
We can clearly observe two distinct parts:
- The first half consists of odd numbers from 1 to 9.
- The second half consists of even numbers from 2 to 10.
This suggests that a single loop must be responsible for generating two different sequences depending on the iteration range.
So instead of thinking in terms of numbers directly, we think in terms of positions in a loop.
Let us assume we iterate from 1 to 10.
| Iteration Range | Output Pattern |
|---|---|
| 1 to 5 | Odd numbers |
| 6 to 10 | Even numbers |
This observation becomes the foundation of the solution.
Deriving the Odd Number Formula
We first focus on generating:
1, 3, 5, 7, 9
Let us map loop values to output values:
| i | Output |
|---|---|
| 1 | 1 |
| 2 | 3 |
| 3 | 5 |
| 4 | 7 |
| 5 | 9 |
Now we look for a mathematical relationship.
A consistent pattern emerges:
- Multiply the index by 2
- Subtract 1
This gives the formula:
2*i - 1
Verification:
- i = 1 → 1
- i = 2 → 3
- i = 3 → 5
- i = 4 → 7
- i = 5 → 9
This confirms that the formula correctly generates the odd sequence.
Deriving the Even Number Formula
Next, we focus on:
2, 4, 6, 8, 10
These correspond to iterations 6 to 10.
To simplify, we shift the index so that it starts from zero:
| i | i - 5 |
|---|---|
| 6 | 1 |
| 7 | 2 |
| 8 | 3 |
| 9 | 4 |
| 10 | 5 |
Now we observe a familiar pattern again: multiples of 2.
So the formula becomes:
2 * (i - 5)
Verification:
- i = 6 → 2
- i = 7 → 4
- i = 8 → 6
- i = 9 → 8
- i = 10 → 10
This successfully generates the even sequence.
Combining Both Conditions in a Single Loop
Now that both formulas are derived, the next step is to combine them using conditional logic.
The key idea is:
- For the first half of the loop (1 to 5), generate odd numbers.
- For the second half (6 to 10), generate even numbers.
This leads to the condition:
- If
i <= 5, use odd number formula. - Otherwise, use even number formula.
Final Implementation
i = 1
while i <= 10:
if i <= 5:
print(2*i - 1)
else:
print(2*(i - 5))
i = i + 1
above mentioned code ouput will be
1
3
5
7
9
2
4
6
8
10
like this
but to get a exact output
we have to modify it like below mentioned
Code :
Output :
Flow of Execution
The logic can be understood in terms of decision flow:
- Start with i = 1
- Check if i is less than or equal to 5
- If true, generate odd number using
2*i - 1 - If false, generate even number using
2*(i - 5) - Print the result
- Increment i
- Repeat until i exceeds 10
This ensures that a single loop is responsible for producing two distinct sequences.
Flowchart Representation
Flowchart showing how a single loop splits into odd and even number generation logic.
Key Learnings
This problem is not about loops alone. It is fundamentally about pattern recognition and decomposition.
The key takeaways include:
1. Break the problem into segments
Instead of trying to solve the full sequence at once, divide it into smaller logical parts.
2. Identify index-to-value mapping
Every output number is a transformation of the loop variable.
3. Use conditional logic carefully
A single loop can simulate multiple patterns if conditions are properly defined.
4. Avoid premature coding
Attempting to code without understanding the pattern leads to confusion and inefficiency.
Conclusion
Although this problem appears simple, it demonstrates an important principle in programming: complex output patterns can often be reduced to simple mathematical relationships when broken down properly.
The challenge is not writing the loop itself, but understanding how to transform a single variable into multiple sequences through logic.



Top comments (0)