Count Down with a While Loop: flowchart, algorithm and program

This program reads a number n and prints every number from n down to 1, one per line, then prints "Lift off". It uses a while loop that keeps running as long as n is at least 1, and lowers n by one each time.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Read n.
  3. Step 3: If n >= 1, go to step 4; otherwise go to step 6.
  4. Step 4: Print n.
  5. Step 5: Set n = n - 1. Go to step 3.
  6. Step 6: Print "Lift off".
  7. Step 7: Stop.

Pseudocode

BEGIN
    READ n
    WHILE n >= 1 DO
        PRINT n
        n ← n - 1
    END WHILE
    PRINT "Lift off"
END

Program in C, Python and Java

Count Down with a While Loop program in C

#include <stdio.h>

int main() {
    int n;
    printf("Enter n: ");
    scanf("%d", &n);
    while (n >= 1) {
        printf("%d\n", n);
        n--;
    }
    printf("Lift off\n");
    return 0;
}

Count Down with a While Loop program in Python

n = int(input("Enter n: "))
while n >= 1:
    print(n)
    n -= 1
print("Lift off")

Count Down with a While Loop program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter n: ");
        int n = sc.nextInt();
        while (n >= 1) {
            System.out.println(n);
            n--;
        }
        System.out.println("Lift off");
    }
}

Sample input: n = 5
Output: 5 4 3 2 1 Lift off

How it works

  • scanf stores the number typed by the user in n.
  • The while condition n >= 1 is checked before every round. If it is false at the start (for example n = 0), the loop body never runs.
  • Inside the loop, printf prints the current value of n, and then n-- reduces n by 1.
  • When n becomes 0, the condition n >= 1 fails and the loop stops.
  • Only after the loop ends does printf print "Lift off", so it appears once, at the end.

Dry run (trace table)

StepLineBoxnOutput
13Start
26Read n5Enter n:
37n >= 1?5
48Print n55
59n = n - 14
67n >= 1?4
78Print n44
89n = n - 13
97n >= 1?3
108Print n33
119n = n - 12
127n >= 1?2
138Print n22
149n = n - 11
157n >= 1?1
168Print n11
179n = n - 10
187n >= 1?0
1911Print "Lift off"0Lift off
2012Stop0

Time and space complexity

Time: O(n) · Space: O(1). The loop runs n times doing constant work each time, using only one variable.

Common mistakes

  • Forgetting n-- inside the loop. The value of n never changes, the condition stays true, and the program prints the same number forever (infinite loop).
  • Using n > 1 instead of n >= 1, which stops one step early and never prints 1.
  • Putting the "Lift off" printf inside the loop body, which prints it after every number instead of once at the end.

Viva questions

Q1. What does this program do?

It reads a number n, prints every number from n down to 1 on separate lines, and then prints 'Lift off'.

Q2. What does the condition n >= 1 in the while loop check, and why not n > 1?

It checks that n is still at least 1, so the loop keeps printing until 1 is printed. With n > 1 the number 1 would never be printed.

Q3. What is the purpose of n-- on line 9, and what happens if you remove it?

It decreases n by 1 each time so the loop moves towards ending. Without it n never changes, the condition stays true, and the loop runs forever printing the same number.

Q4. What is the output if the user enters 0 or a negative number like -3?

The condition n >= 1 is false at the start, so the loop body never runs and only 'Lift off' is printed.

Q5. What is the output if the user enters 1?

The loop runs exactly once, printing 1, then n becomes 0, the loop stops, and 'Lift off' is printed.

Q6. Why is a while loop a good choice here rather than a do-while loop?

A while loop checks the condition before the first iteration, so it correctly prints nothing for n <= 0. A do-while would always print at least once, which would wrongly print 0 or a negative number.

Q7. Why do we write &n in scanf, and what is the value of n after the loop ends?

scanf needs the address of n so it can store the input value there. After the loop ends n is 0, because the original value has been decremented down to 0.

Q8. What is the time complexity of this program?

O(n), because the loop body runs once for each number from n down to 1, so the work grows in direct proportion to n.

Q9. What would you change to count up from 1 to n instead?

Use a separate counter i starting at 1, loop while i <= n, print i, and do i++ each time. You need a new variable because n is the upper limit and must not be decreased.

Frequently asked questions

What happens if the user enters 0 or a negative number?

The condition n >= 1 is false right away, so the loop body is skipped and the program prints only "Lift off".

Why do we use a while loop instead of a for loop here?

We don't know in advance how many times to repeat, since it depends on n, and a while loop fits that well. A for loop would work too, but while shows clearly the condition, the print and the update as separate steps.

Why is n-- placed after the printf?

If n-- came first, the program would print n-1 on the first round and skip the original n. Printing first and then decrementing keeps the output starting at n.

More programs

← All programs

Try AI Flowchart free →