Given a number n, return the count of digits in this number.
Example:
Input: n = 1567
Output: 4
Explanation: There are 4 digits in 1567, which are 1, 5, 6 and 7.Input: n = 255
Output: 3
Explanation: There are 3 digits in 255, which are 2, 5 and 5.
Table of Content
- Iterative Solution -O(log10(n)) Time or O(Number of digits) and O(1) Space
- Using Recursion - O(log10(n)) or O(Number of digits) Time and O(Number of digits) Space
- Using log base 10 function - O(1) Time and O(1) Space
- Converting Number to String - O(Number of digits) Time and O(Number of digits) Space

Iterative Solution -O(log10(n)) Time or O(Number of digits) and O(1) Space
The idea is to count the digits by removing them one by one from the rightmost side (least significant digit) until the number becomes 0.
The rightmost digit can be removed easily by performing integer division by 10. Repeat this process and increment the count each time a digit is removed.
#include <bits/stdc++.h>
using namespace std;
int countDigits(int n) {
if (n == 0)
return 1;
n = abs(n);
int count = 0;
// Remove one digit at a time
while (n > 0) {
n /= 10;
count++;
}
return count;
}
int main() {
int n = 1567;
cout << countDigits(n);
return 0;
}
class GFG {
static int countDigits(int n) {
if (n == 0)
return 1;
n = Math.abs(n);
int count = 0;
// Remove one digit at a time
while (n > 0) {
n /= 10;
count++;
}
return count;
}
public static void main(String[] args) {
int n = 1567;
System.out.println(countDigits(n));
}
}
def countDigits(n):
if n == 0:
return 1
n = abs(n)
count = 0
# Remove one digit at a time
while n > 0:
n //= 10
count += 1
return count
if __name__ == "__main__":
n = 1567
print(countDigits(n))
using System;
class GFG
{
static int countDigits(int n)
{
if (n == 0)
return 1;
n = Math.Abs(n);
int count = 0;
// Remove one digit at a time
while (n > 0)
{
n /= 10;
count++;
}
return count;
}
static void Main()
{
int n = 1567;
Console.WriteLine(countDigits(n));
}
}
function countDigits(n) {
if (n === 0)
return 1;
n = Math.abs(n);
let count = 0;
// Remove one digit at a time
while (n > 0) {
n = Math.floor(n / 10);
count++;
}
return count;
}
// Driver code
let n = 1567;
console.log(countDigits(n));
Output
4
Using Recursion - O(log10(n)) or O(Number of digits) Time and O(Number of digits) Space
The idea is to remove digits from right by calling a recursive function for each digit.
- The base condition of this recursive approach is when we divide the number by 10 and the number gets reduced to 0, so return 1 for this operation.
- If not base case, keep dividing the number by 10 this reduces the input number size by 1 and keeps track of the number of sizes reduced.
#include <bits/stdc++.h>
using namespace std;
int countDigits(int n) {
n = abs(n);
if (n < 10)
return 1;
// Remove one digit and recurse
return 1 + countDigits(n / 10);
}
int main() {
int n = 1567;
cout << countDigits(n);
return 0;
}
class GFG {
static int countDigits(int n) {
n = Math.abs(n);
if (n < 10)
return 1;
// Remove one digit and recurse
return 1 + countDigits(n / 10);
}
public static void main(String[] args) {
int n = 1567;
System.out.println(countDigits(n));
}
}
def countDigits(n):
n = abs(n)
if n < 10:
return 1
# Remove one digit and recurse
return 1 + countDigits(n // 10)
if __name__ == "__main__":
n = 1567
print(countDigits(n))
using System;
class GFG
{
static int countDigits(int n)
{
n = Math.Abs(n);
if (n < 10)
return 1;
// Remove one digit and recurse
return 1 + countDigits(n / 10);
}
static void Main()
{
int n = 1567;
Console.WriteLine(countDigits(n));
}
}
function countDigits(n) {
n = Math.abs(n);
if (n < 10)
return 1;
// Remove one digit and recurse
return 1 + countDigits(Math.floor(n / 10));
}
// Driver code
let n = 1567;
console.log(countDigits(n));
Output
4
Using log base 10 function - O(1) Time and O(1) Space
We can use log10(logarithm of base 10) to count the number of digits of positive numbers (logarithm is not defined for negative numbers).
For negative numbers, first take the absolute value of n, since logarithm is not defined for negative values.
Digit count of n = floor(log10(n) + 1)
#include <bits/stdc++.h>
using namespace std;
int countDigits(int n) {
n = abs(n);
if (n == 0)
return 1;
// Log gives the number of digits indirectly
return floor(log10(n)) + 1;
}
int main() {
int n = 1567;
cout << countDigits(n);
return 0;
}
class GFG {
static int countDigits(int n) {
n = Math.abs(n);
if (n == 0)
return 1;
// Log gives the number of digits indirectly
return (int) Math.floor(Math.log10(n)) + 1;
}
public static void main(String[] args) {
int n = 1567;
System.out.println(countDigits(n));
}
}
import math
def countDigits(n):
n = abs(n)
if n == 0:
return 1
# Log gives the number of digits indirectly
return math.floor(math.log10(n)) + 1
if __name__ == "__main__":
n = 1567
print(countDigits(n))
using System;
class GFG
{
static int countDigits(int n)
{
n = Math.Abs(n);
if (n == 0)
return 1;
// Log gives the number of digits indirectly
return (int)Math.Floor(Math.Log10(n)) + 1;
}
static void Main()
{
int n = 1567;
Console.WriteLine(countDigits(n));
}
}
function countDigits(n) {
n = Math.abs(n);
if (n === 0)
return 1;
// Log gives the number of digits indirectly
return Math.floor(Math.log10(n)) + 1;
}
// Driver code
let n = 1567;
console.log(countDigits(n));
Output
4
Converting Number to String - O(Number of digits) Time and O(Number of digits) Space
We convert the number into a string and then find the length of the string to get the number of digits in the original number.
If the number is negative, the string will contain a
'-'sign, so subtract 1 from the length.
#include <bits/stdc++.h>
using namespace std;
int countDigits(int n) {
n = abs(n);
// Convert number to string
string s = to_string(n);
return s.length();
}
int main() {
int n = 1567;
cout << countDigits(n);
return 0;
}
class GFG {
static int countDigits(int n) {
n = Math.abs(n);
// Convert number to string
String s = String.valueOf(n);
return s.length();
}
public static void main(String[] args) {
int n = 1567;
System.out.println(countDigits(n));
}
}
def countDigits(n):
n = abs(n)
# Convert number to string
s = str(n)
return len(s)
if __name__ == "__main__":
n = 1567
print(countDigits(n))
using System;
class GFG
{
static int countDigits(int n)
{
n = Math.Abs(n);
// Convert number to string
string s = n.ToString();
return s.Length;
}
static void Main()
{
int n = 1567;
Console.WriteLine(countDigits(n));
}
}
function countDigits(n) {
n = Math.abs(n);
// Convert number to string
let s = n.toString();
return s.length;
}
// Driver code
let n = 1567;
console.log(countDigits(n));
Output
4