Count Digits is a classic warm-up problem: given a non-negative integer N
, determine how many digits it contains. Although trivial to humans, it teaches loop control, integer math, and logarithms—skills that resurface in harder interview questions.
🔹 Problem Statement
Input: A single non-negative integer N
(0 ≤ N ≤ 1018).
Output: The count of decimal digits in N
.
Examples: N = 0 → 1
(one digit), N = 5012 → 4
, N = 1018 − 1 → 18
.
🔹 Naive Loop : Count Digits in C++
Strip digits one-by-one with division until the number becomes 0.
#include <iostream>
using i64 = long long; // 64-bit range fits 1018
int main() {
i64 n; std::cin >> n;
if (n == 0) { std::cout << 1; return 0; }
int cnt = 0;
while (n != 0) { // remove least-significant digit
n /= 10;
++cnt; // increase count
}
std::cout << cnt;
}
📝 Dry-Run (N = 5012)
n = 5012
→ cnt = 0
loop 1: n = 501
, cnt = 1
loop 2: n = 50
, cnt = 2
loop 3: n = 5
, cnt = 3
loop 4: n = 0
, cnt = 4
→ stop. Output 4.
Time: O(d)
In terms of n, Since d = ⌊log₁₀(n)⌋ + 1, it can also be expressed as O(log n).
Space: O(1)
Acceptable but we can do better.
🔹 String Convert : Count Digits in C++
Convert number to string and take .size()
. Simpler code, same complexity.
#include <iostream>
#include <string>
int main() {
std::string s; std::cin >> s; // read as string
std::cout << s.size(); // length = digit count
}
📝 Dry-Run (N = 0)
Input line read as "0\"
; s.size() = 1
→ print 1.
Time: O(d)
In terms of n, Since d = ⌊log₁₀(n)⌋ + 1, it can also be expressed as O(log n).
Space: O(1)
Readable but string conversion costs memory and is disallowed in some contests.
🔹Logarithmic Formula : Count Digits in C++
A positive integer N has ⌊log10(N)⌋ + 1
digits. Handle N = 0 separately.
#include <iostream>
#include <cmath>
using i64 = long long;
int main() {
i64 n; std::cin >> n;
int digits = (n == 0) ? 1
: static_cast<int>(std::floor(std::log10(n))) + 1;
std::cout << digits;
}
🔹 Understanding the Logarithmic Formula
One of the fastest ways to count digits in a number is using the logarithm formula. In C++, it looks like this:
int digits = (n == 0) ? 1 : static_cast<int>
(std::floor(std::log10(n))) + 1;
👉 Step-by-Step Explanation
- Ternary Operator
(condition ? value_if_true : value_if_false)
: – Ifn == 0
, we directly return 1 because zero has exactly one digit. - When n > 0:
std::log10(n)
→ finds the base-10 logarithm ofn
. Example:log10(9999) ≈ 3.9999
.std::floor(...)
→ rounds this down to the nearest integer. Example:floor(3.9999) = 3
.- Adding
+1
→ gives the total number of digits. Example:3 + 1 = 4
.
static_cast<int>
: – Sincelog10
returns a floating-point number, we convert it safely to anint
.
📝 Dry Run Examples
Case 1: n = 0
Condition true → digits = 1 ✅
Case 2: n = 9,999
log10(9999) ≈ 3.9999 → floor = 3 → add 1 = 4 digits ✅
Case 3: n = 100
log10(100) = 2 → floor = 2 → add 1 = 3 digits ✅
✅ Alternative (Beginner Friendly)
To make it easier for beginners, the same logic can be written with an if–else
instead of the compact ternary operator:
int digits;
if (n == 0)
{
digits = 1; // special case: 0 has one digit
}
else
{
digits = static_cast<int>(std::floor(std::log10(n))) + 1;
}
This version is easier to read for beginners, while the one-liner with the ternary operator is compact and often preferred in competitive programming.
Time: O(1)
Space: O(1)
Fastest approach—preferred in interviews when you explain the math.
🔹 Takeaways
- Start simple (loop), discuss trade-offs, then improve.
- String trick is clean but uses extra memory.
- Math shortcut with
log10
gives O(1) time—mention floating-point edge cases but still works up to 1018.
📝 Try it Yourself: Modify the loop method to count digits in any base B (2 ≤ B ≤ 16). What change is required?
Mastering this problem helps you practice stepwise optimization: from brute force to optimal, while always discussing complexity and trade-offs—exactly what interviewers want to hear.