Beginner · loops
Count Down
Every loop you have written started at 1 and went up. Nothing requires that: the start, the condition and the step are three independent choices, and this problem changes two of them.
At a glance<code>for (int i = n; i >= 1; i--)</code> prints each i separated by a space, and a newline at the end.
Problem
Input: One integer n (1 ≤ n ≤ 100).
Output: n down to 1, separated by single spaces, on one line.
Samples:
| Input | Output |
|---|---|
5 | 5 4 3 2 1 |
1 | 1 |
The n = 1 sample matters: it is the case where the loop runs exactly once and no separator logic can go wrong. If that one fails, the bug is in the loop bounds, not in the spaces.
Your solution
Custom input for "Run my input"
Judging runs in your browser against hidden tests. A runaway loop is cut off after 3 seconds — still, make sure every loop has an exit. Your code is saved in this browser as you type.
Stuck? Hints
Hint 1 — Flip the three parts
Counting up was i = 1; i <= n; i++. Counting down is i = n; i >= 1; i-- — start high, continue while at least 1, step down. Two changes: the comparison direction and the step sign. They must change together or the loop never ends.
Hint 2 — Spaces between, not after
Print a space before every number except the first — or print the number then a space for all but the last. Either works; what fails is printing a space after every number including the last, if the judge trims inconsistently. This site trims line ends, but the habit is right.
Hint 3 — The n = 1 trap
With n = 1 the loop runs once and prints one number with no separators at all. Any separator logic that assumes "at least two items" breaks here. Run the second sample before you trust your formatting.
Solution (try it first!)
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = n; i >= 1; i--) {
cout << i;
if (i > 1) cout << " ";
}
cout << endl;
return 0;
}
Down-counting loops are rarer but appear exactly where output order matters: reversing digits in Reverse a Number, undoing a stack in Balanced Brackets, and printing a number's bits in Binary Representation. Reversed boundaries are also where most off-by-one bugs live.
Deep dive
Why this works
A for loop's three parts are independent choices: start value, continuation condition, and step. Counting down just means starting high, testing >= 1, and stepping with --. The loop shape is identical; only the direction flipped.
The cost
O(n) time and O(1) extra space — n iterations, one number and possibly one space per iteration.
Where it goes wrong
- Changing only one of direction and step:
i = n; i >= 1; i++never terminates, because the condition stays true forever. - Separator logic that assumes at least two items. With n = 1 the loop prints one number and no separator at all — run that sample.
- Testing
i > 0versusi >= 1: both work for integers, but mixing them with unsigned types or float steps introduces subtle differences. Pick one convention and stay with it.
Next step
Reversed iteration is everywhere output order matters: Reverse a Number, Binary Representation, Balanced Brackets.
Two things to remember
- Start, condition and step are independent — a for loop can walk in either direction.
- Direction and step sign must change together, or the loop never ends.
🐞 This version fails — open it and try
Stops at 2 — the condition should allow the value 1 through
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = n; i > 1; i--) { // bug: drops the final 1
cout << i << " ";
}
cout << endl;
return 0;
}