Given a number N, the task is to return the count of digits in this number.
Example:

Program to count digits in an integer
Simple Iterative Solution to count digits in an integer
The integer entered by the user is stored in the variable n. Then the while loop is iterated until the test expression n != 0 is evaluated to 0 (false). We will consider 3456 as the input integer.
- After the first iteration, the value of n will be updated to 345 and the count is incremented to 1.
- After the second iteration, the value of n will be updated to 34 and the count is incremented to 2.
- After the third iteration, the value of n will be updated to 3 and the count is incremented to 3.
- In the fourth iteration, the value of n will be updated to zero and the count will be incremented to 4.
- Then the test expression is evaluated ( n!=0 ) as false and the loop terminates with final count as 4.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countDigit( long long n)
{
if (n == 0)
return 1;
int count = 0;
while (n != 0) {
n = n / 10;
++count;
}
return count;
}
int main( void )
{
long long n = 345289467;
cout << "Number of digits : " << countDigit(n);
return 0;
}
|
C
#include <stdio.h>
int countDigit( long long n)
{
if (n == 0)
return 1;
int count = 0;
while (n != 0) {
n = n / 10;
++count;
}
return count;
}
int main( void )
{
long long n = 345289467;
printf ( "Number of digits : %d" , countDigit(n));
return 0;
}
|
Java
import java.io.*;
public class GFG {
static int countDigit( long n)
{
int count = 0 ;
while (n != 0 ) {
n = n / 10 ;
++count;
}
return count;
}
public static void main(String[] args)
{
long n = 345289467 ;
System.out.print( "Number of digits : "
+ countDigit(n));
}
}
|
Python3
def countDigit(n):
count = 0
while n ! = 0 :
n / / = 10
count + = 1
return count
n = 345289467
print ( "Number of digits : % d" % (countDigit(n)))
|
C#
using System;
class GFG {
static int countDigit( long n)
{
int count = 0;
while (n != 0) {
n = n / 10;
++count;
}
return count;
}
public static void Main()
{
long n = 345289467;
Console.WriteLine( "Number of"
+ " digits : " + countDigit(n));
}
}
|
PHP
<?php
function countDigit( $n )
{
$count = 0;
while ( $n != 0)
{
$n = round ( $n / 10);
++ $count ;
}
return $count ;
}
$n = 345289467;
echo "Number of digits : "
. countDigit( $n );
?>
|
Javascript
<script>
function countDigit(n)
{
let count = 0;
while (n != 0)
{
n = Math.floor(n / 10);
++count;
}
return count;
}
n = 345289467;
document.write( "Number of digits : " + countDigit(n));
</script>
|
Output
Number of digits : 9
Time Complexity : O(log10(n)) or O(num digits)
Auxiliary Space: O(1) or constant
Recursive Solution to count digits in an integer
Keep dividing the number by 10 this reduces the input number size by 1 and keeps track of the number of sizes reduced.
Algorithm:
- 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.
- Make a function call by dividing the number by 10, reducing the input size of the given number by 1, and adding 1 for this operation.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countDigit( long long n)
{
if (n/10 == 0)
return 1;
return 1 + countDigit(n / 10);
}
int main( void )
{
long long n = 345289467;
cout << "Number of digits :" << countDigit(n);
return 0;
}
|
C
#include <stdio.h>
int countDigit( long long n)
{
if (n/10 == 0)
return 1;
return 1 + countDigit(n / 10);
}
int main( void )
{
long long n = 345289467;
printf ( "Number of digits : %d" , countDigit(n));
return 0;
}
|
Java
import java.util.*;
class GFG {
static int countDigit( long n)
{
if (n/ 10 == 0 )
return 1 ;
return 1 + countDigit(n / 10 );
}
public static void main(String[] args)
{
long n = 345289467 ;
System.out.print( "Number of digits : "
+ countDigit(n));
}
}
|
Python3
def countDigit(n):
if n / / 10 = = 0 :
return 1
return 1 + countDigit(n / / 10 )
n = 345289467
print ( "Number of digits : % d" % (countDigit(n)))
|
C#
using System;
class GFG {
static int countDigit( long n)
{
if (n/10 == 0)
return 1;
return 1 + countDigit(n / 10);
}
public static void Main()
{
long n = 345289467;
Console.WriteLine( "Number of "
+ "digits : "
+ countDigit(n));
}
}
|
PHP
<?php
function countDigit( $n )
{
if ( $n /10 == 0)
return 1;
return 1 + countDigit((int)( $n / 10));
}
$n = 345289467;
print ( "Number of digits : " .
(countDigit( $n )));
?>
|
Javascript
<script>
function countDigit(n)
{
if (n/10 == 0)
return 1;
return 1 + countDigit(parseInt(n / 10));
}
var n = 345289467;
document.write( "Number of digits :" + countDigit(n));
</script>
|
Output
Number of digits :9
Time Complexity : O(log(n))
Auxiliary Space : O(log(n))
Log-based Solution to count digits in an integer
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 = upper bound of log10(N).
Below is the implementation of the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
int countDigit( long long n) {
return floor ( log10 (n) + 1);
}
int main( void )
{
long long n = 345289467;
cout << "Number of digits : "
<< countDigit(n);
return 0;
}
|
C
#include <math.h>
#include <stdio.h>
int countDigit( long long n) {
return floor ( log10 (n) + 1);
}
int main( void )
{
long long n = 345289467;
printf ( "Number of digits : %d" , countDigit(n));
return 0;
}
|
Java
import java.util.*;
class GFG {
static int countDigit( long n)
{
return ( int )Math.floor(Math.log10(n) + 1 );
}
public static void main(String[] args)
{
long n = 345289467 ;
System.out.print( "Number of digits : "
+ countDigit(n));
}
}
|
Python3
import math
def countDigit(n):
return math.floor(math.log10(n) + 1 )
n = 345289467
print ( "Number of digits : % d" % (countDigit(n)))
|
C#
using System;
class GFG {
static int countDigit( long n)
{
return ( int )Math.Floor(Math.Log10(n) + 1);
}
public static void Main()
{
long n = 345289467;
Console.WriteLine( "Number of digits : "
+ countDigit(n));
}
}
|
PHP
<?php
function countDigit( $n )
{
return floor (log10( $n )+1);
}
$n = 345289467;
echo "Number of digits : " ,
countDigit( $n );
?>
|
Javascript
<script>
function countDigit(n)
{
return Math.floor(Math.log10(n) + 1);
}
var n = 345289467;
document.write( "Number of digits : " +
countDigit(n));
</script>
|
Output
Number of digits : 9
Time Complexity: O(1) or constant
Auxiliary Space: O(1) or constant
Converting given number to string solution to count digits in an integer
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.
Note: It gives TLE for numbers other than range of int,example long and long long .
C++
#include <bits/stdc++.h>
using namespace std;
void count_digits( int n)
{
string num = to_string(n);
cout << num.size() << endl;
}
int main()
{
int n = 345;
count_digits(n);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static void count_digits( int n)
{
String num = Integer.toString(n);
System.out.println(+num.length());
}
public static void main(String args[])
{
int n = 345 ;
count_digits(n);
}
}
|
Python3
def count_digits(n):
n = str (n)
return len (n)
n = 456533457776
print (count_digits(n))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void count_digits( int n)
{
string num = Convert.ToString(n);
Console.WriteLine(+num.Length);
}
public static void Main( string [] args)
{
int n = 345;
count_digits(n);
}
}
|
Javascript
<script>
function count_digits(n)
{
let num = n.toString();
document.write(num.length);
}
let n = 345;
count_digits(n);
</script>
|
Time Complexity: O(1) or constant
Auxiliary Space: O(Number of digits in an integer)
This article is contributed by Suruchi Kumari . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
16 Feb, 2023
Like Article
Save Article