Q. Write a algorithm and program to count the number of digits in a number.

A number is a mathematical object used to count, measure, and label. For eg 4, 5, 12, etc are numbers. Our task is to count the number of digits in the given number. Here we will see its algorithm and program. Let us understand the question with the help of an example.

For Example :- Suppose an input number is given as 12345 then the output will be 5 as there are 5 digits in the given number and digits are 1, 2, 3, 4, 5
Input :- 12345
Output :- 5

Algorithm to count the number of digits in a given number

START
step 1 : Input a number from user. Store it in some variable say num.
step 2 : Initialize another variable to store total digits say digit = 0.
step 3 : If num > 0 then increment count by 1 i.e. count++.
step 4 : Divide num by 10 to remove last digit of the given number i.e. num = num / 10.
step 5 : Repeat step 3 to 4 till num > 0 or num != 0.
STOP


Program to count the number of digits in a number

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • #include <stdio.h>
    int count_digit(long long num) 
    { 
    int count = 0; 
    while (num != 0) { 
    num = num / 10; 
    ++count; 
    } 
    return count; 
    } 
    // Driver code 
    int main(void) 
    { 
    long long num = 874512369; 
    printf("Number of digits : %d", 
    count_digit(num)); 
    return 0; 
    } 
    
    #include <iostream>
    using namespace std; 
    int count_digit(long long num) 
    { 
    int count = 0; 
    while (num != 0) { 
    num = num / 10; 
    ++count; 
    } 
    return count; 
    } 
    // Driver code 
    int main(void) 
    { 
    long long num = 874512369; 
    cout << "Number of digits : "
    << count_digit(num); 
    return 0; 
    } 
    
    class LFC { 
    static int count_digit(long num) 
    { 
    int count = 0; 
    while (num != 0) { 
    num = num / 10; 
    ++count; 
    } 
    return count; 
    } 
    /* Driver program to test above function */
    public static void main(String[] args) 
    { 
    long num = 874512369; 
    System.out.print("Number of digits : " + count_digit(num)); 
    } 
    } 
    
    def count_digit(num): 
    count = 0
    while num != 0: 
    num //= 10
    count+= 1
    return count 
    # Driver Code     
    num = 874512369
    print ("Number of digits : % d"%(count_digit(num))) 
    
    using System; 
    class LFC { 
    static int count_digit(long num) 
    { 
    int count = 0; 
    while (num != 0) { 
    num = num / 10; 
    ++count; 
    } 
    return count; 
    } 
    /* Driver program to test 
    above function */
    public static void Main() 
    { 
    long num = 874512369; 
    Console.WriteLine("Number of"
    + " digits : " + count_digit(num)); 
    } 
    } 
    
    function count_digit($num) 
    { 
    $count = 0; 
    while ($num != 0)  
    { 
    $num = round($num / 10); 
    ++$count; 
    } 
    return $count; 
    } 
    // Driver code 
    $num = 874512369; 
    echo "Number of digits : "
    . count_digit($num); 
    
    #include <stdio.h>
    int count_digit(long long num) 
    { 
    int count = 0; 
    while (num != 0) { 
    num = num / 10; 
    ++count; 
    } 
    return count; 
    } 
    // Driver code 
    int main(void) 
    { 
    long long num = 874512369; 
    printf("Number of digits : %d", 
    count_digit(num)); 
    return 0; 
    } 
    

    Output

    Number of digits : 9
    




    Recommended Programs

       Program to find factorial of a number
       Program to check strings are anagram or not