0
0
CppProgramBeginner · 2 min read

C++ Program to Count Digits in a Number

You can count digits in a number in C++ by repeatedly dividing the number by 10 inside a while loop and incrementing a counter until the number becomes 0, like this: 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 divide the number by 10 before it becomes zero. Each division removes the last digit, so counting these divisions tells you how many digits the number has. Handle zero as a special case since dividing zero won't enter the loop.
📐

Algorithm

1
Get the input number.
2
If the number is zero, return 1 as the digit count.
3
If the number is negative, convert it to positive.
4
Initialize a counter to zero.
5
While the number is not zero, divide it by 10 and increment the counter.
6
Return the counter as the number of digits.
💻

Code

cpp
#include <iostream>
using namespace std;

int main() {
    int num, count = 0;
    cout << "Enter a number: ";
    cin >> num;

    if (num == 0) {
        count = 1;
    } else {
        if (num < 0) num = -num;
        while (num != 0) {
            num /= 10;
            count++;
        }
    }

    cout << "Number of digits: " << count << endl;
    return 0;
}
Output
Enter a number: 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, continue

3

Check if negative

num is positive, no change

4

Start loop

num = 12345, count = 0

5

Divide and count

num = 1234, count = 1

6

Divide and count

num = 123, count = 2

7

Divide and count

num = 12, count = 3

8

Divide and count

num = 1, count = 4

9

Divide and count

num = 0, count = 5

10

Loop ends

num is zero, exit loop

numcount
123450
12341
1232
123
14
05
💡

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: Make number positive

If the number is negative, convert it to positive because digit count is the same for negative numbers.

Step 3: Count digits by division

Each time we divide the number by 10, we remove the last digit and increase the count until the number becomes zero.

🔄

Alternative Approaches

Convert number to string and count length
cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
    int num;
    cout << "Enter a number: ";
    cin >> num;
    string s = to_string(num < 0 ? -num : num);
    cout << "Number of digits: " << s.length() << endl;
    return 0;
}
This method is simple and uses built-in string length but uses extra memory for the string.
Use logarithm to count digits
cpp
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int num;
    cout << "Enter a number: ";
    cin >> num;
    if (num == 0) {
        cout << "Number of digits: 1" << endl;
    } else {
        num = abs(num);
        int count = (int)log10(num) + 1;
        cout << "Number of digits: " << count << endl;
    }
    return 0;
}
This method is very fast but requires including <cmath> and careful handling of zero.

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

Time Complexity

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

Space Complexity

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

Which Approach is Fastest?

The division loop and logarithm methods are both fast; the string method uses extra memory and is slower for very large numbers.

ApproachTimeSpaceBest For
Division loopO(d)O(1)Simple and memory efficient
String conversionO(d)O(d)Easy to implement, less efficient
Logarithm methodO(1)O(1)Fastest for large numbers, needs math library
💡
Remember to handle zero and negative numbers separately when counting digits.
⚠️
Beginners often forget to handle zero as a special case, resulting in a digit count of zero.