Countdown
Print inside a loop that counts down. An easy embedded C problem (Decisions and loops), run and graded in the browser.
easy · Decisions and loops · loops · printf
Print the numbers from n down to 1, each followed by a space, then the word liftoff and a newline.
For n equal to 3 the output is 3 2 1 liftoff. For 0 it is just liftoff.
Starter code
void countdown(int n) {
printf("liftoff\n");
}Example tests
From three
countdown(3);Expected output:
3 2 1 liftoffFrom one
countdown(1);Expected output:
1 liftoffLearn it first
- Decisions and loops · Choosing with if and switch, repeating with while and for, and the loop that never ends.
More Decisions and loops problems
- The larger of two · Choose between two values with an if statement.
- Sign of a number · Handle three cases with if, else if and else.
- Keep a value in range · Limit a value to a minimum and a maximum.
- Sum the numbers up to n · Repeat an addition with a loop.
- Count the digits · Loop until a value runs out.
- Raise to a power · Multiply repeatedly inside a loop.
- Days in a month · Pick one of many cases with a switch statement.
- Fix: the heater never turns on · A comparison that is really an assignment.
- Fix: the countdown never ends · A loop counting down to zero with an unsigned counter can never stop.
- Fix: the maximum is always the last reading · A semicolon after an if condition turns the update into a statement that always runs.
- Greenhouse: switch the pump with a band · Decide whether the pump runs, with a band so the relay does not chatter.