Repair a digit-counting algorithm and justify termination

The pseudocode is intended to count decimal digits in a nonnegative integer, including one digit for 0. Identify both independent defects, repair the algorithm, and show a dry-run for 5070. Explain why your repaired algorithm terminates; a corrected final answer without that reasoning is incomplete.

    Translate your repair into a standalone C program reading one integer n in 0..1000000000. Print digits=D, or invalid for missing/out-of-range input. Use an explicit special case for zero and a while loop for positive values. The reference uses long for the input so the stated upper bound is within the minimum range guaranteed for that type.

Assume every integer token supplied is representable in long.

INPUT n
    digits ← 0
    WHILE n > 0
        next ← n DIV 10
        digits ← digits + 1
    END WHILE
    OUTPUT digits

