Given a number n, the task is to 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 256, which are 2, 5 and 5.
[Approach 1] Iterative Solution
The idea is to count the digits by removing the digits from the input number starting from right(least significant digit) to left(most significant digit) till the number is reduced to 0. We are removing digits from the right because the rightmost digit can be removed simply by performing integer division by 10. For eg: n = 1567, then 1567 / 10 = 156.7 = 156(Integer Division).
C++
#include <bits/stdc++.h>
using namespace std;
int countDigit(int n)
{
// Base case
if (n == 0)
return 1;
int count = 0;
// Iterate till n has digits remaining
while (n != 0)
{
// Remove rightmost digit
n = n / 10;
// Increment digit count by 1
++count;
}
return count;
}
int main()
{
int n = 58964;
cout << countDigit(n);
return 0;
}
C
#include <stdio.h>
int countDigit(int n)
{
// Base case
if (n == 0)
return 1;
int count = 0;
// Iterate till n has digits remaining
while (n != 0)
{
// Remove rightmost digit
n = n / 10;
// Increment digit count by 1
++count;
}
return count;
}
int main()
{
int n = 58964;
printf("%d\n", countDigit(n));
return 0;
}
Java
import java.io.*;
class GfG {
static int countDigit(int n)
{
// Base case
if (n == 0)
return 1;
int count = 0;
// Iterate till n has digits remaining
while (n != 0) {
// Remove rightmost digit
n = n / 10;
// Increment digit count by 1
++count;
}
return count;
}
public static void main(String[] args)
{
int n = 58964;
System.out.println(countDigit(n));
}
}
Python
def countDigit(n):
# Base case
if n == 0:
return 1
count = 0
# Iterate till n has digits remaining
while n != 0:
# Remove rightmost digit
n = n // 10
# Increment digit count by 1
count += 1
return count
if __name__ == "__main__":
n = 58964
print(countDigit(n))
C#
using System;
class GfG {
static int CountDigit(int n)
{
// Base case
if (n == 0)
return 1;
int count = 0;
// Iterate till n has digits remaining
while (n != 0) {
// Remove rightmost digit
n = n / 10;
// Increment digit count by 1
++count;
}
return count;
}
static void Main()
{
int n = 58964;
Console.WriteLine(CountDigit(n));
}
}
JavaScript
function countDigit(n)
{
// Base case
if (n === 0)
return 1;
let count = 0;
// Iterate till n has digits remaining
while (n !== 0) {
// Remove rightmost digit
n = Math.floor(n / 10);
// Increment digit count by 1
++count;
}
return count;
}
let n = 58964;
console.log(countDigit(n));
Time Complexity - O(log10(n)) or O(Number of digits)
Space Complexity - O(1)
[Approach 2] Removing digits using Recursion
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. Otherwise, Keep dividing the number by 10 this reduces the input number size by 1 and keeps track of the number of sizes reduced.
C++
#include <bits/stdc++.h>
using namespace std;
// Recursively count the number of digits in the integer 'n'
int countDigit(int n)
{
// Base case: if 'n' is a single digit
if (n / 10 == 0)
return 1;
// Recursive case: strip one digit and count
return 1 + countDigit(n / 10);
}
int main()
{
int n = 58964;
cout << countDigit(n);
return 0;
}
C
#include <stdio.h>
// Recursively count the number of digits in the integer 'n'
int countDigit(int n)
{
// Base case: if 'n' is a single digit
if (n / 10 == 0)
return 1;
// Recursive case: strip one digit and count
return 1 + countDigit(n / 10);
}
int main()
{
int n = 58964;
printf("%d", countDigit(n));
return 0;
}
Java
import java.util.*;
class GfG {
// Recursively counts the number of digits in the number 'n'
static int countDigit(int n)
{
// Base case: if 'n' is a single-digit number
if (n / 10 == 0)
return 1;
// Recursive call: strip one digit and count
return 1 + countDigit(n / 10);
}
public static void main(String[] args)
{
int n = 58964;
System.out.print(countDigit(n));
}
}
Python
def countDigit(n):
# Base case: if 'n' is a single-digit number
if n // 10 == 0:
return 1
# Recursive case: strip one digit and count
return 1 + countDigit(n // 10)
if __name__ == "__main__":
n = 58964
print(countDigit(n))
C#
using System;
class GfG {
// Recursively counts the number of digits in the number 'n'
static int countDigit(int n)
{
// Base case: if 'n' is a single-digit number
if (n / 10 == 0)
return 1;
// Recursive call: strip one digit and count
return 1 + countDigit(n / 10);
}
public static void Main()
{
int n = 58964;
Console.WriteLine(countDigit(n));
}
}
JavaScript
function countDigit(n) {
// Base case: if 'n' is a single-digit number
if (parseInt(n / 10) === 0)
return 1;
// Recursive case: strip one digit and count
return 1 + countDigit(parseInt(n / 10));
}
var n = 58964;
console.log(countDigit(n));
Time Complexity - O(log10(n)) or O(Number of digits)
Space Complexity - O(1)
[Approach 3] Using log base 10 function
We can use log10(logarithm of base 10) to count the number of digits of positive numbers (logarithm is not defined for negative numbers).
Digit count of n = floor(log10(n) + 1)
C++
#include <bits/stdc++.h>
using namespace std;
int countDigit(int n)
{
// Use logarithm base 10 to count digits
// log10(n) gives number of digits minus 1, so add 1
return floor(log10(n) + 1);
}
int main()
{
int n = 58964;
cout << countDigit(n);
return 0;
}
C
#include <math.h>
#include <stdio.h>
int countDigit(int n)
{
// Use log base 10 to count the number of digits
// log10(n) gives digits - 1, so add 1
return floor(log10(n) + 1);
}
int main()
{
int n = 58964;
printf("%d\n", countDigit(n));
return 0;
}
Java
import java.util.*;
class GfG {
// Uses logarithm base 10 to count digits in 'n'
static int countDigit(int n)
{
// log10(n) gives digits - 1, so add 1 and floor the result
return (int)Math.floor(Math.log10(n) + 1);
}
public static void main(String[] args)
{
int n = 58964;
System.out.print(countDigit(n));
}
}
Python
import math
def countDigit(n):
# Use logarithm base 10 to count digits
# log10(n) gives digits - 1, so add 1 and floor the result
return math.floor(math.log10(n) + 1)
if __name__ == "__main__":
n = 58964
print(countDigit(n))
C#
using System;
class GfG {
// Counts the number of digits using logarithm base 10
static int countDigit(int n)
{
// log10(n) gives digits - 1, so add 1 and floor the result
return (int)Math.Floor(Math.Log10(n) + 1);
}
public static void Main()
{
int n = 58964;
Console.WriteLine(countDigit(n));
}
}
JavaScript
function countDigit(n) {
// Use logarithm base 10 to count the number of digits
// log10(n) gives digits - 1, so add 1 and floor the result
return Math.floor(Math.log10(n) + 1);
}
var n = 58964;
console.log(countDigit(n));
Time Complexity - O(1)
Space Complexity - O(1)
[Approach 4] Converting Number to String
We can convert the number into a string and then find the length of the string to get the number of digits in the original number.
C++
#include <bits/stdc++.h>
using namespace std;
int countDigit(int n)
{
// Convert the integer to a string
string num = to_string(n);
// Return the length of the string (i.e., number of digits)
return num.length();
}
int main()
{
int n = 58964;
cout << countDigit(n);
return 0;
}
Java
class Main {
// Counts the number of digits by converting the number to a string
static int countDigit(int n)
{
// Convert number to string
String num = Long.toString(n);
return num.length();
}
public static void main(String[] args)
{
int n = 58964;
System.out.println(countDigit(n));
}
}
Python
def count_digit(n):
# Convert the number to a string
num = str(n)
# Return the length of the string (number of digits)
return len(num)
n = 58964
print(count_digit(n))
C#
using System;
class GfG {
static int CountDigit(int n)
{
// Convert the number to a string
string num = n.ToString();
// Return the length of the string (number of digits)
return num.Length;
}
public static void Main()
{
int n = 58964;
Console.WriteLine(CountDigit(n));
}
}
JavaScript
function countDigit(n)
{
// Convert the number to a string
let num = n.toString();
// Return the length of the string (number of digits)
return num.length;
}
let n = 58964;
console.log(countDigit(n));
Time Complexity - O(1)
Space Complexity - O(Number of digits)
Count Digits in Python
Program to Count Digits in an Integer
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem