Count digits in a number

Problem Statement: Given an integer N , write program to count number of digits in N.

Examples:

Example 1:
Input: N = 12345
Output: 5
Explanation: N has 5 digits

Example 2:
Input: N = 8394
Output: 4
Explanation: N has 4 digits

Disclaimer: Don’t jump directly to the solution, try it out yourself first.

Approach

  1. Store the integer in a variable X and initialize a counter variable to count the number of digits.
  2. We know that in programming languages when we divide X by Y it will result in an integer (given both the variables are integers). For example, 133/10 will result in 13 similarly 1/10 will result in 0.
  3. Using a for loop and above observation keep on dividing X by 10 and increment the count in every iteration when X equals 0 terminate the loop and the count will have the number of digits in N.

Code:

C++ Code

#include<bits/stdc++.h>

using namespace std;

int count_digits( int n )
{
   int x = n; int count =0;
   while( x !=0 ) 
   {
       x = x / 10;
       count++;
   }
   return count;
}


int main()
{
   int n = 12345;
   cout<< "Number of digits in "<<n<<" is "<<count_digits(n);
   return 0;
}

Output: Number of digits in 12345 is 5

Time Complexity: O (n) where n is the number of digits in the given integer

Space Complexity: O(1)

Java Code

public class tUf { 
    static int count_digits(int n)
    {
       int x = n; int count =0;
       while( x!=0 ) 
      {
          x = x / 10;
          count++;
      }
       return count;
    } 
    public static void main(String args[]) 
    { 
         int n = 12345;
        System.out.println("Number of digits in "+n+" is "+count_digits(n));
    } 
}

Output: Number of digits in 12345 is 5

Time Complexity: O (n) where n is the number of digits in the given integer

Space Complexity: O(1)

Python Code

def count_digits(n):
       count=0
       x=n
       while( x != 0 ):
               x//=10
               count+=1
       return count

n = 12345
print("Number of digits in ",n," is ",count_digits(n)) 

Output: Number of digits in 12345 is 5

Time Complexity: O (n) where n is the number of digits in the given integer

Space Complexity: O(1)

Solution 2 : 

  1. Convert the integer into a string.
  2. Find the length of the string      

Code:

C++ Code

#include<bits/stdc++.h>

using namespace std;

int count_digits( int n )
{
  string x = to_string(n);
  return x.length(); 
}

int main()
{
   int n = 12345;
   cout<< "Number of digits in "<<n<<" is "<<count_digits(n);
   return 0;
}

Output: Number of digits in 12345 is 5

Time Complexity: O (1) 

Space Complexity: O(1)

Java Code

import java.util.*;
public class tUf {
  static int count_digits(int n) {
    String n2 = Integer.toString(n);
    return n2.length();
  }

  public static void main(String args[]) {
    int n = 12345;
    System.out.println("Number of digits in " + n + " is " + count_digits(n));
  }
}

Output: Number of digits in 12345 is 5

Time Complexity: O (1) 

Space Complexity: O(1)

Python Code

def count_digits(n):
  x = str(n)
return len(x)

n = 12345
print("Number of digits in ", n, " is ", count_digits(n))

Output: Number of digits in 12345 is 5

Time Complexity: O (1) 

Space Complexity: O(1)

Solution 3 : 

Use logarithm base 10 to count the number of digits. As

The number of digits in an integer = the upper bound of log10(n).

Example :

n = 12345
log10(12345) = 4.091
Integral part of 4.091 is 4 and 4 + 1 =  5 which is also the number of digits in 12345               

Code:

C++ Code

#include<bits/stdc++.h>

using namespace std;

int count_digits( int n )
{
  int digits = floor(log10(n) + 1);
  return digits;
}

int main()
{
   int n = 12345;
   cout<< "Number of digits in "<<n<<" is "<<count_digits(n);
   return 0;
}

Output: Number of digits in 12345 is 5

Time Complexity: O (1) 

Space Complexity: O(1)

Java Code

import java.util.*;
public class tUf { 
    static int count_digits(int n)
    {
       int digits = (int) Math.floor(Math.log10(n) + 1);
        return digits;
    } 

    public static void main(String args[]) 
    { 
         int n = 12345;
        System.out.println("Number of digits in "+n+" is "+count_digits(n));
    } 
}

Output: Number of digits in 12345 is 5

Time Complexity: O (1) 

Space Complexity: O(1)

Python Code

import math
def count_digits(n):
  digits = math.floor(math.log10(n) + 1)
return digits
n = 12345
print("Number of digits in ", n, " is ", count_digits(n))

Output: Number of digits in 12345 is 5

Time Complexity: O (1) 

Space Complexity: O(1)

Special thanks to Harsh Verma for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article