✓ Link copied to clipboard
C 📅 July 5, 2026 ⏱ 5 min read

C Program to Count Number of Digits in an Integer

C Program to Count Number of Digits in an Integer

Counting digits in an integer is a common programming problem. For instance, if you take 12345, it'll show you it has 5 digits. This task helps when you're processing numerical data.

Prerequisites for Counting Digits in C

Before writing the C program, ensure you know these:

  • <stdio.h> for input and output functions.
  • The while loop to iterate over the digits.
  • Integer division using / to remove the last digit.
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler.

Source Code of C Program to Count Number of Digits in an Integer

Here's the C code to count the number of digits in an integer:

digits_counter.cC▶ Run
#include <stdio.h>

int main() {
    int n, count = 0;
    // Read an integer from the user
    printf("Enter an integer: ");
    scanf("%d", &n);

    // Continue until n becomes 0
    while (n != 0) {
        n /= 10;
        count++;
    }

    printf("The number of digits is: %d\n", count);
    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Count Digits Program

When you run the program with 12345 as input, you'll see:

output.txttext
Enter an integer: 12345
The number of digits is: 5
Output of C Program to Count Number of Digits in an Integer
Output of C Program to Count Number of Digits in an Integer

Algorithm to Count Digits in C

Counting the number of digits requires repeatedly removing the last digit until none are left. This is how the logic unfolds:

  1. Read the integer n from the user.
  2. Initialize count to 0.
  3. Repeat while n is not zero:
  4. Divide n by 10 to strip the last digit.
  5. Increment count by 1 for each division.
  6. End repeat.
flowchart TD
  A([Start]) --> B[/Read n/]
  B --> C[count = 0]
  C --> D{"n != 0?"}
  D -->|Yes| E["n = n / 10"]
  E --> F["count = count + 1"]
  F --> D
  D -->|No| G([End])
Flowchart: the algorithm step by step

How the Count Digits Program Works

Let's go through the code line by line:

  • #include <stdio.h>: Includes the Standard Input Output library for functions like printf and scanf.
  • The main() function: Entry point of the program.
  • Declares int n, count = 0;: Initializes count to store the number of digits.
  • printf("Enter an integer: "): Prompts the user to enter a number.
  • scanf("%d", &n): Reads the integer input from the user.
  • The while (n != 0) loop: Continues as long as n is not zero.
  • n /= 10;: Removes the last digit of n.
  • count++;: Increments the digit count for every digit removed.
  • printf("The number of digits is: %d\n", count);: Displays the final count.
  • return 0;: Ends the program successfully.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What loop is suitable for counting digits in an integer?

🎉Correct! While loops are great for running when you don't know how many times you'll need to loop beforehand.
🤔Not quite. Think about a loop that continues as long as a condition is true.

Time and Space Complexity of the Count Digits Program

Analyzing the performance of this program involves understanding both time and space aspects.

Time Complexity: O(log n). The symbol n here represents the actual integer input length — for a number like 12345, n is 5. The loop while (n != 0) divides n by 10 each time, reducing its size logarithmically. So, with 10 digits, about 10 iterations occur.

Space Complexity: O(1). The program uses only a fixed number of integer variables: n and count, making the memory usage constant regardless of the input size.

⚠️

Common mistake: Forgets to account for negative numbers, leading to wrong counts if not handled separately.

Key takeaway: Dividing by 10 is a simple, effective way to truncate a number, letting you count digits efficiently.

FAQs About Counting Digits in C

Here are some common questions about counting digits in an integer using C.

The program won't count any digits since it loops until n hits zero. You might wanna add a check for zero as a special case.
Not directly. To manage negatives, think about taking the absolute value of n before the loop.
Convert the float to a string, then loop through each character, leaving out the decimal point, to count the digits.
The program can handle any number of digits fitting within an int, usually up to 10 digits for 32-bit integers.
A while loop’s more flexible here since it runs based on a condition, which works great for counting digits until n becomes zero.

If you found this program helpful, check out these related programs:

Conclusion

Counting digits by dividing by 10 is a neat trick seen in many coding problems. It's good to practice with negative numbers and zero to cover those edge cases. You’ll find similar logic used for digit extraction in various algorithms.

Ready for more? Test your skills with our C language quiz or run more programs in our C online compiler.