Program to count digits in an integer (4 Different Methods) - GeeksforGeeks

20 Aug, 2021
Count the number of digits in a long integer entered by a user.
Simple Iterative Solution 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). 
After the first iteration, the value of n will be 345 and the count is incremented to 1.After the second iteration, the value of n will be 34 and the count is incremented to 2.After the third iteration, the value of n will be 3 and the count is incremented to 3.At the start of the fourth iteration, the value of n will be 0 and the loop is terminated.
After the first iteration, the value of n will be 345 and the count is incremented to 1.
After the second iteration, the value of n will be 34 and the count is incremented to 2.
After the third iteration, the value of n will be 3 and the count is incremented to 3.
At the start of the fourth iteration, the value of n will be 0 and the loop is terminated.
Then the test expression is evaluated for false and the loop terminates. 
C++
C
Java
Python3
C#
PHP
Javascript
// Iterative C++ program to count// number of digits in a number#include <bits/stdc++.h>using namespace std; int countDigit(long long n){    int count = 0;    while (n != 0)    {        n = n / 10;        ++count;    }    return count;} // Driver codeint main(void){    long long n = 345289467;    cout << "Number of digits : " << countDigit(n);    return 0;} // This code is contributed// by Akanksha Rai
// Iterative C program to count number of// digits in a number#include <stdio.h> int countDigit(long long n){    int count = 0;    while (n != 0)    {        n = n / 10;        ++count;    }    return count;} // Driver codeint main(void){    long long n = 345289467;    printf("Number of digits : %d", countDigit(n));    return 0;}
// JAVA Code to count number of// digits in an integerclass GFG {     static int countDigit(long n)    {        int count = 0;        while (n != 0) {            n = n / 10;            ++count;        }        return count;    }     /* Driver code */    public static void main(String[] args)    {        long n = 345289467;        System.out.print("Number of digits : "                         + countDigit(n));    }}// This code is contributed by Arnav Kr. Mandal.
# Iterative Python program to count# number of digits in a number  def countDigit(n):    count = 0    while n != 0:        n //= 10        count += 1    return count  # Driver Coden = 345289467print("Number of digits : % d" % (countDigit(n))) # This code is contributed by Shreyanshi Arun
// C# Code to count number of// digits in an integerusing System; class GFG {     static int countDigit(long n)    {        int count = 0;        while (n != 0)        {            n = n / 10;            ++count;        }        return count;    }     /* Driver code */    public static void Main()    {        long n = 345289467;        Console.WriteLine("Number of"                          + " digits : " + countDigit(n));    }} // This code is contributed by anuj_67.
<?php// Iterative PHP program to count// number of digits in a number function countDigit($n){    $count = 0;    while ($n != 0)    {        $n = round($n / 10);        ++$count;    }    return $count;} // Driver code$n = 345289467;echo "Number of digits : "        . countDigit($n); //This code is contributed by mits?>
<script> // Iterative Javascript program to count// number of digits in a number function countDigit(n){    let count = 0;    while (n != 0)    {        n = Math.floor(n / 10);        ++count;    }    return count;} // Driver code     n = 345289467;    document.write("Number of digits : "+ countDigit(n));     // This code is contributed by Mayank Tyagi </script>
Number of digits : 9
Recursive Solution: 
C++
C
Java
Python3
C#
PHP
Javascript
// Recursive C++ program to count number of// digits in a number#include <bits/stdc++.h>using namespace std; int countDigit(long long n){    if (n/10 == 0)        return 1;    return 1 + countDigit(n / 10);} // Driver codeint main(void){    long long n = 345289467;    cout << "Number of digits :" << countDigit(n);    return 0;}// This code is contributed by Mukul Singh.
// Recursive C program to count number of// digits in a number#include <stdio.h> int countDigit(long long n){    if (n/10 == 0)        return 1;    return 1 + countDigit(n / 10);} // Driver codeint main(void){    long long n = 345289467;    printf("Number of digits : %d", countDigit(n));    return 0;}
// JAVA Code to count number of// digits in an integerimport java.util.*; class GFG {     static int countDigit(long n)    {        if (n/10 == 0)            return 1;        return 1 + countDigit(n / 10);    }     /* Driver code */    public static void main(String[] args)    {        long n = 345289467;        System.out.print("Number of digits : "                         + countDigit(n));    }} // This code is contributed by Arnav Kr. Mandal.
# Recursive Python program to count# number of digits in a number  def countDigit(n):    if n/10 == 0:        return 1    return 1 + countDigit(n // 10)  # Driver Coden = 345289467print("Number of digits : % d" % (countDigit(n))) # This code is contributed by Shreyanshi Arun
// C# Code to count number of// digits in an integerusing System; class GFG {     static int countDigit(long n)    {        if (n/10 == 0)            return 1;        return 1 + countDigit(n / 10);    }     /* Driver Code */    public static void Main()    {        long n = 345289467;        Console.WriteLine("Number of "                          + "digits : "                          + countDigit(n));    }} // This code is contributed by anuj_67.
<?php// Recursive PHP program to count// number of digits in a number function countDigit($n){    if ($n/10 == 0)        return 1;    return 1 + countDigit((int)($n / 10));} // Driver Code$n = 345289467;print ("Number of digits : " .            (countDigit($n))); // This code is contributed by mits?>
<script> // Recursive Javascript program to count number of// digits in a number function countDigit(n){    if (n/10 == 0)        return 1;    return 1 + countDigit(parseInt(n / 10));} // Driver codevar n = 345289467;document.write("Number of digits :" + countDigit(n)); </script>
Number of digits :9
Log-based Solution: 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). 
C++
C
Java
Python3
C#
PHP
Javascript
// Log based C++ program to count number of// digits in a number#include <bits/stdc++.h>using namespace std; int countDigit(long long n) {  return floor(log10(n) + 1);} // Driver codeint main(void){    long long n = 345289467;    cout << "Number of digits : "         << countDigit(n);    return 0;} // This code is contributed by shivanisinghss2110
// Log based C program to count number of// digits in a number#include <math.h>#include <stdio.h> int countDigit(long long n) {  return floor(log10(n) + 1);} // Driver codeint main(void){    long long n = 345289467;    printf("Number of digits : %d", countDigit(n));    return 0;}
// JAVA Code to count number of// digits in an integerimport java.util.*; class GFG {     static int countDigit(long n)    {        return (int)Math.floor(Math.log10(n) + 1);    }     /* Driver code */    public static void main(String[] args)    {        long n = 345289467;        System.out.print("Number of digits : "                         + countDigit(n));    }}// This code is contributed by Arnav Kr. Mandal.
# Log based Python program to count number of# digits in a number # function to import ceil and logimport math  def countDigit(n):    return math.floor(math.log10(n)+1)  # Driver Coden = 345289467print("Number of digits : % d" % (countDigit(n))) # This code is contributed by Shreyanshi Arun
// C# Code to count number of// digits in an integerusing System; class GFG {     static int countDigit(long n)    {        return (int)Math.Floor(Math.Log10(n) + 1);    }     /* Driver code */    public static void Main()    {        long n = 345289467;        Console.WriteLine("Number of digits : "                          + countDigit(n));    }} // This code is contributed by anuj_67..
<?php// Log based php program to// count number of digits// in a number function countDigit($n){    return floor(log10($n)+1);} // Driver code$n = 345289467;echo "Number of digits : ",    countDigit($n); // This code is contributed by ajit?>
<script> // Log based Javascript program to count number of// digits in a numberfunction countDigit(n){    return Math.floor(Math.log10(n) + 1);} // Driver codevar n = 345289467;document.write("Number of digits : "  +               countDigit(n));                // This code is contributed by noob2000 </script>
Number of digits : 9
Method 4: 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++
Java
Python3
C#
Javascript
#include <bits/stdc++.h>using namespace std; // To count the no. of digits in a numbervoid count_digits(int n){    // converting number to string using    // to_string in C++    string num = to_string(n);     // calculate the size of string    cout << num.size() << endl;}//Driver Codeint main(){    // number    int n = 345;    count_digits(n);    return 0;} // This code is contributed by Shashank Pathak
import java.util.*;public class GFG {     // To count the no. of digits in a number    static void count_digits(int n)    {        // converting number to string using        // to_string in C++        String num = Integer.toString(n);         // calculate the size of string         System.out.println(+num.length());    }    // Driver code    public static void main(String args[])    {        // number        int n = 345;        count_digits(n);    }}// Code is contributed by shivanisinghss2110
# Python3 implementation of the approachdef count_digits(n):    n = str(n)    return len(n)  # Driver coden = 456533457776print(count_digits(n))
// C# implementation of the above approachusing System;using System.Collections.Generic; class GFG {     // To count the no. of digits in a number    static void count_digits(int n)    {        // converting number to string using        // to_string in C#         string num = Convert.ToString(n);         // calculate the size of string        Console.WriteLine(+num.Length);    }     // Driver Code    public static void Main(string[] args)    {        // number        int n = 345;        count_digits(n);    }} // This code is contributed by shivanisinghss2110
<script>    // Javascript implementation of the above approach         // To count the no. of digits in a number    function count_digits(n)    {        // converting number to string using        // to_string in javascript          let num = n.toString();          // calculate the size of string        document.write(num.length);    }         // number    let n = 345;    count_digits(n);     </script>
3
This article is contributed by Vishal Kumar Gupta. 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. 
Mithun Kumar
jit_t
vt_m
endisnear
jatin739
Akanksha_Rai
Code_Mech
AyushGupta8
Shashank_Pathak
shivanisinghss2110
itsmohitj
mayanktyagi1709
rutvik_56
noob2000
user282
suresh07
bunnyram19
abhijeetgawai2000
number-digits
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find sum of elements in a given array
Program for factorial of a number
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java