Program to count digits in an integer (3 Different Methods)

1.5

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 variable n. Then the while loop is iterated until the test expression n != 0 is evaluated to 0 (false).

  1. After first iteration, the value of n will be 345 and the count is incremented to 1.
  2. After second iteration, the value of n will be 34 and the count is incremented to 2.
  3. After third iteration, the value of n will be 3 and the count is incremented to 3.
  4. After fourth iteration, the value of n will be 0 and the count is incremented to 4.

Then the test expression is evaluated to false and the loop terminates.

C

// 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 code
int main(void)
{
    long long n = 345289467;
    printf("Number of digits : %d",
           countDigit(n));
    return 0;
}

Java

// JAVA Code to count number of
// digits in an integer
class GFG {
    
    static int countDigit(long n)
    {
        int count = 0;
        while (n != 0) {
            n = n / 10;
            ++count;
        }
        return count;
    }
    
    /* Driver program to test above function */
    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.


Output:

Number of digits : 9

Recursive Solution:

C

// Recursive C program to count number of
// digits in a number
#include <stdio.h>

int countDigit(long long n)
{
    if (n == 0)
        return 0;
    return 1 + countDigit(n / 10);
}

// Driver code
int main(void)
{
    long long n = 345289467;
    printf("Number of digits : %d",
           countDigit(n));
    return 0;
}

Java

// JAVA Code to count number of
// digits in an integer
import java.util.*;

class GFG {
    
    static int countDigit(long n)
    {
        if (n == 0)
            return 0;
        return 1 + countDigit(n / 10);
    }
    
    /* Driver program to test above function */
    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.    


Output:

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

// Log based C program to count number of
// digits in a number
#include <stdio.h>
#include <math.h>

int countDigit(long long n)
{
    return ceil(log10(n));
}

// Driver code
int main(void)
{
    long long n = 345289467;
    printf("Number of digits : %d",
           countDigit(n));
    return 0;
}

Java

// JAVA Code to count number of
// digits in an integer
import java.util.*;

class GFG {
    
    static int countDigit(long n)
    {
        return (int)Math.ceil(Math.log10(n));
    }
    
    /* Driver program to test above function */
    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.


Output:

Number of digits : 9

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 contribute@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.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



1.5 Average Difficulty : 1.5/5.0
Based on 4 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.