Count the Digits: flowchart, algorithm and program

This program reads an integer n and prints how many digits it has. It keeps dividing n by 10 and counts how many divisions it takes to reach 0, since each division removes one digit.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Set count = 0.
  3. Step 3: Read n.
  4. Step 4: If n < 0, go to step 5; otherwise go to step 6.
  5. Step 5: Set n = -n.
  6. Step 6: If n == 0, go to step 7; otherwise go to step 8.
  7. Step 7: Set count = 1.
  8. Step 8: If n > 0, go to step 9; otherwise go to step 11.
  9. Step 9: Increase count by 1.
  10. Step 10: Set n = n / 10. Go to step 8.
  11. Step 11: Print count.
  12. Step 12: Stop.

Pseudocode

BEGIN
    count ← 0
    READ n
    IF n < 0 THEN
        n ← -n
    END IF
    IF n == 0 THEN
        count ← 1
    END IF
    WHILE n > 0 DO
        count++
        n ← n / 10
    END WHILE
    PRINT count
END

Program in C, Python and Java

Count the Digits program in C

#include <stdio.h>

int main() {
    int n, count = 0;
    printf("Enter n: ");
    scanf("%d", &n);
    if (n < 0)
        n = -n;
    if (n == 0)
        count = 1;
    while (n > 0) {
        count++;
        n = n / 10;
    }
    printf("Number of digits: %d\n", count);
    return 0;
}

Count the Digits program in Python

n = int(input("Enter n: "))
count = 0
if n < 0:
    n = -n
if n == 0:
    count = 1
while n > 0:
    count += 1
    n = n // 10
print("Number of digits:", count)

Count the Digits program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n, count = 0;
        System.out.print("Enter n: ");
        n = sc.nextInt();
        if (n < 0)
            n = -n;
        if (n == 0)
            count = 1;
        while (n > 0) {
            count++;
            n = n / 10;
        }
        System.out.println("Number of digits: " + count);
    }
}

Sample input: n = 12345
Output: Number of digits: 5

How it works

  • If n is negative, n = -n makes it positive, because the minus sign is not a digit.
  • If n is 0, count is set to 1 straight away. The while loop never runs for 0, so without this the answer would wrongly be 0.
  • Each pass of while (n > 0) does count++ and then n = n / 10. Integer division drops the last digit.
  • For n = 12345, n becomes 1234, 123, 12, 1, 0 over five passes, so count ends at 5 and the loop stops.
  • The final count is printed as the number of digits.

Dry run (trace table)

StepLineBoxcountnOutput
13Start
24count = 00
36Read n012345Enter n:
47n < 0?012345
59n == 0?012345
611n > 0?012345
712count++112345
813n = n / 1011234
911n > 0?11234
1012count++21234
1113n = n / 102123
1211n > 0?2123
1312count++3123
1413n = n / 10312
1511n > 0?312
1612count++412
1713n = n / 1041
1811n > 0?41
1912count++51
2013n = n / 1050

Time and space complexity

Time: O(log n) · Space: O(1). The loop divides n by 10 each time, so it runs about log10(n) times using only a few variables.

Common mistakes

  • Forgetting the n == 0 case, so 0 prints 'Number of digits: 0' instead of 1.
  • Not handling negative numbers. With n < 0 the loop condition n > 0 is false at once and count stays 0.
  • Using n = n % 10 instead of n / 10. The remainder gives the last digit, but it does not remove it from n.

Viva questions

Q1. Why is count initialised to 0 at the start?

count is a running total that gets incremented once per digit. Starting at 0 means no digits have been counted yet, so the first increment makes it 1.

Q2. What does n = n / 10 do, and why does it help count digits?

Both operands are int, so it is integer division and drops the last digit (for example 345 becomes 34). Each division removes one digit, so the number of divisions until n reaches 0 equals the number of digits.

Q3. Why do you have the separate check if (n == 0) count = 1?

If n is 0, the while condition n > 0 is false and the loop never runs, so count would stay 0. But 0 has one digit, so we set count to 1 by hand.

Q4. What is the purpose of if (n < 0) n = -n?

The loop condition is n > 0, so a negative number would skip the loop and give a wrong count. Taking the absolute value first lets -345 be counted as 3 digits, since the minus sign is not a digit.

Q5. Why did you use a while loop and not a for loop?

We don't know in advance how many times the loop will run, because it depends on the number of digits. A while loop that runs until n becomes 0 fits this naturally.

Q6. Trace the program for n = 1200. What is the output?

n goes 1200, 120, 12, 1, 0, which is 4 iterations, so it prints 4. Trailing zeros are counted as digits because each division by 10 removes one regardless of its value.

Q7. What is the time complexity of this program?

O(log n), more precisely O(d) where d is the number of digits, because n is divided by 10 on each iteration. The loop runs about log10(n) + 1 times.

Q8. What would change if you wanted to count the digits in binary, or what could go wrong for the smallest int value?

For binary you would divide by 2 instead of 10, and count would then be the number of bits. Separately, for INT_MIN (-2147483648), -n overflows because the positive value does not fit in an int, so the behaviour is undefined. Using long long or unsigned for n would avoid this.

Frequently asked questions

Why do we divide by 10 to count digits?

In integer division, n / 10 removes the last digit of n. Counting how many times you can do this before n reaches 0 gives the number of digits.

What does this program print for 0?

It prints 1. The special case if (n == 0) sets count = 1, because the while loop does not run when n is 0.

Does it work for negative numbers?

Yes. The line n = -n turns a negative number into a positive one first, so -999 gives 3 digits, the same as 999.

More programs

← All programs

Try AI Flowchart free →