0
0
CProgramBeginner · 2 min read

C Program to Count Digits in a Number

To count digits in a number in C, use a loop that divides the number by 10 repeatedly and increments a counter until the number becomes 0, like while (num != 0) { num /= 10; count++; }.
📋

Examples

Input12345
OutputNumber of digits: 5
Input0
OutputNumber of digits: 1
Input-9876
OutputNumber of digits: 4
🧠

How to Think About It

To count digits, think of how many times you can remove the last digit by dividing by 10 until nothing remains. Each division removes one digit, so counting these divisions gives the total digits. Handle zero and negative numbers by treating their absolute value and considering zero as one digit.
📐

Algorithm

1
Get the input number.
2
If the number is zero, return 1 as the digit count.
3
Convert the number to its absolute value to handle negatives.
4
Initialize a counter to zero.
5
While the number is greater than zero, divide it by 10 and increment the counter.
6
Return the counter as the number of digits.
💻

Code

c
#include <stdio.h>
#include <stdlib.h>

int main() {
    int num, count = 0;
    printf("Enter an integer: ");
    scanf("%d", &num);

    if (num == 0) {
        count = 1;
    } else {
        num = abs(num);
        while (num > 0) {
            num /= 10;
            count++;
        }
    }

    printf("Number of digits: %d\n", count);
    return 0;
}
Output
Enter an integer: 12345 Number of digits: 5
🔍

Dry Run

Let's trace the input 12345 through the code to count digits.

1

Input number

num = 12345, count = 0

2

Check if zero

num is not zero, proceed

3

Convert to absolute

num = 12345 (already positive)

4

Divide and count loop

Loop 1: num=12345/10=1234, count=1 Loop 2: num=1234/10=123, count=2 Loop 3: num=123/10=12, count=3 Loop 4: num=12/10=1, count=4 Loop 5: num=1/10=0, count=5

5

Loop ends

num=0, count=5

Iterationnumcount
112341
21232
3123
414
505
💡

Why This Works

Step 1: Handle zero input

If the number is zero, it has exactly one digit, so we set count to 1 immediately.

Step 2: Use absolute value

Negative numbers are converted to positive using abs() so digit counting works the same.

Step 3: Count digits by division

Dividing the number by 10 removes the last digit each time, so counting how many times this happens until zero gives the total digits.

🔄

Alternative Approaches

String conversion
c
#include <stdio.h>
#include <string.h>

int main() {
    char str[20];
    printf("Enter an integer: ");
    scanf("%s", str);
    int length = strlen(str);
    int count = (str[0] == '-') ? length - 1 : length;
    printf("Number of digits: %d\n", count);
    return 0;
}
This method counts digits by converting the number to a string and counting characters, ignoring the minus sign if present. It is simpler but uses extra memory.
Recursive counting
c
#include <stdio.h>
#include <stdlib.h>

int countDigits(int num) {
    num = abs(num);
    if (num < 10) return 1;
    return 1 + countDigits(num / 10);
}

int main() {
    int num;
    printf("Enter an integer: ");
    scanf("%d", &num);
    printf("Number of digits: %d\n", countDigits(num));
    return 0;
}
This uses recursion to count digits by breaking down the number until it is less than 10. It is elegant but uses more stack memory.

Complexity: O(d) time, O(1) space

Time Complexity

The loop runs once for each digit in the number, so time grows linearly with the number of digits, O(d).

Space Complexity

Only a few variables are used, so space complexity is constant, O(1).

Which Approach is Fastest?

The division loop is fastest and uses least memory compared to string conversion or recursion.

ApproachTimeSpaceBest For
Division loopO(d)O(1)Efficient and simple digit counting
String conversionO(d)O(d)Easy to implement but uses extra memory
Recursive countingO(d)O(d)Elegant but uses stack memory
💡
Remember to handle zero and negative numbers separately when counting digits.
⚠️
Beginners often forget to handle zero as a special case, resulting in zero digits counted.