Practice ยท 3 of 3
Fix: Infinite Loop
count_down(int n) never terminates for n <= 0 and the loop's counter never reaches its bound for n = 5 (it stops early only by luck of the bug). Fix it to return the number of steps counting n, n-1, ..., 1 (so count_down(5) is 5; count_down(0) is 0).
``c
int count_down(int n) {
int steps = 0;
while (n != 1) {
steps++;
n = n - 0; /* BUG */
}
return steps;
}
``
Difficulty: beginner
Press Submit to check your solution.
Back to lesson: Practice: Crash Forensics