Count Digits Recursively in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>

int countDigits(int n) {
    if (n == 0) return 0;
    return 1 + countDigits(n / 10);
}

int main() {
    int num = 54321;
    printf("Number of digits: %d\n", countDigits(num));
    return 0;
}

C Output

Input:
54321

Output:
Number of digits: 5


C++ Program

#include <iostream>
using namespace std;

int countDigits(int n) {
    if (n == 0) return 0;
    return 1 + countDigits(n / 10);
}

int main() {
    int num = 9876;
    cout << "Number of digits: " << countDigits(num) << endl;
    return 0;
}

C++ Output

Input:
9876

Output:
Number of digits: 4


JAVA Program

public class Main {
    static int countDigits(int n) {
        if (n == 0) return 0;
        return 1 + countDigits(n / 10);
    }

    public static void main(String[] args) {
        int num = 12045;
        System.out.println("Number of digits: " + countDigits(num));
    }
}

JAVA Output

Input:
12045

Output:
Number of digits: 5


Python Program

def count_digits(n):
    if n == 0:
        return 0
    return 1 + count_digits(n // 10)

num = 1001
print("Number of digits:", count_digits(num))

Python Output

Input:
1001

Output:
Number of digits: 4


Explanation
Example
In recursively counting digits, we aim to decompose the number by continuously dividing it by 10. Division tosses out the last digit every time. With each recursive call, one is added to the count. For example, 54321 turns into 5432, then 543, then 54, then 5, and then 0, where the recursion ends and counts backwards.

Real-Life Analogy
Imagine a stack of coins that is very tall. You take the top coin and place it aside, and you do that again and again until you have no coins left. Each time you take out a coin, you count how many there are in total. For recursion, your stack of coins is your number, and taking a coin out is similar to dividing by 10.

Why It Matters
This problem demonstrates how recursion can be used to eliminate loops for digit-based calculations. Most algorithms that operate with numbers (sum of digits, number reverse, or palindrome check) follow the same recursive algorithm. Understanding this enables you to readily transfer to more complicated problems without having to redesign the logic anew.

Complexity and Optimization
The time complexity is O(d) since d is the number of digits, as we iterate over one digit in each recursive call. The space complexity is O(d) because of the call stack. Although constant space would be used in an iterative solution, recursion makes the code simpler and easier for beginners to comprehend breaking problems into sub-problems.

Edge Cases and Testing
If the input number is 0, the code above will output 0 digits. In practical application, you might need to have 0 as having 1 digit. That's easily solved by adding a special case prior to recursion. Negative numbers should also be made positive prior to processing.

Interview Tips
Interviewers frequently employ this problem to determine whether you know how recursion unwinds and the way in which base cases fit. They can also turn it into variations, for example, counting digits in hexadecimal or binary, which still proceeds on the same recursive principle.

SEO-Friendly Closing
Recursion to count digits in C, C++, Java, and Python is an essential practice for mastering the base cases, function call stack, and integer division concepts. It's a precursor to problems such as sum of digits, Armstrong number verification, and number reversal in recursive form. Mastering the concept enhances your problem-solving skills and makes you ready to take programming interviews, competitive programming, and number-based algorithms where recursion offers beautiful, readable solutions.