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

   

C Program

/* C - Count Digits */
#include <stdio.h>
int main(){
    long long n; scanf("%lld",&n);
    if(n<0) n=-n;
    int count = (n==0) ? 1 : 0;
    while(n>0){ count++; n/=10; }
    printf("%d",count);
    return 0;
}

C Output

Input:  
5023 

Output:  
4


C++ Program

// C++ - Count Digits
#include <bits/stdc++.h>
using namespace std;
int main(){
    long long n; cin>>n;
    if(n<0) n=-n;
    int count = (n==0) ? 1 : 0;
    while(n>0){ count++; n/=10; }
    cout<<count;
}

C++ Output

Input:  
123456

Output:  
6


JAVA Program

// Java - Count Digits
import java.util.*;
class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    long n = sc.nextLong();
    if(n<0) n = -n;
    int count = (n==0) ? 1 : 0;
    while(n>0){ count++; n/=10; }
    System.out.print(count);
  }
}

JAVA Output

Input:  
90876 

Output:  
5


Python Program

# Python - Count Digits
n = int(input())
if n < 0:
    n = -n
count = 1 if n == 0 else 0
while n > 0:
    count += 1
    n //= 10
print(count)

Python Output

Input:  
0

Output:  
1


In-Depth Learning – Entire Concept in Paragraphs
Example
If you input 5023, the result is 4 since the number has four digits: 5, 0, 2, and 3. The reasoning keeps on dividing the number by 10 until it is zero, counting the number of times we can do so. Even if the number has a zero in the middle (such as 5023), it is still considered one digit since division removes digits from the right one at a time.

Real-Life Analogy
Imagine this as peeling layers off an onion — every peel you take away is equivalent to cutting off the rightmost digit. You continue peeling until the onion disappears, and the number of peels is equal to the number of digits. Whether or not the onion has a small middle layer (zero) or is large and fat (large numbers), the count of peeling is correct.

Why It Matters
Digit counting may seem trivial, but it's a building block for more advanced problems such as reverse number checking, palindrome checking, Armstrong numbers, and even output formatting. For instance, in constructing financial applications, one can use the count of digits to aid in number formatting (such as including commas). When designing algorithms, digit count is essential in problems that require base conversion or digit-dependent dynamic programming.

Learning Insights
This problem teaches how integer division works, especially when repeatedly dividing by 10. It also reinforces handling special cases like zero (which has 1 digit) and negative numbers (digit count ignores the minus sign). Another takeaway is minimizing unnecessary storage — we count directly instead of converting to strings, which is more memory-efficient and faster in most compiled languages.

Interview Relevance and Practical Use
Interviewers usually employ "Count Digits" to test a candidate's fundamental loop handling and edge case sensitivity. A standard follow-up question is to count digits in a non-loop way (e.g., via logarithms: floor(log10(n))+1), which evaluates mathematical insights. Practical applications involve digit counting in data verification, generating padding for numerical IDs, and verifying constraints in algorithms where the length of digits decides complexity.

Performance and Edge Notes
The time complexity is O(d), with d being the number of digits, since we divide by 10 for every digit. Space complexity is O(1). The code also correctly handles zero and negative numbers. It can be made faster using logarithms but at the cost of the floating-point precision problem, so loop counting is more secure for all integer inputs.

SEO-friendly summary
This C, C++, Java, and Python count digits program is an easy but necessary beginner problem that finds the number of digits in an integer, dealing with positive, negative, and zero values efficiently. By repeatedly dividing the number by 10, the approach refrains from string conversions and maintains constant space complexity. The rationale includes real-world examples, corner cases, and the significance of digit counting in algorithms, data checks, and interview practice, which makes this a programming essential for novices wishing to excel at number-related problems.