Digit Count in C

How to Count Digits in C programming language?

Digit Count in C is a program in which you will learn to count the number of digits in an Integer, which can be both pre-defined or user given.

Digit-Count-in-C

Syntax:

A general code of Digit Count in C
n /= 10;
++count;

Algorithm:

  • Step 1: Start
  • Step 2: Initialize and declare the variable num, i=0.
  • Step 3: Read input num from the user.
  • Step 4:Do step 5 & 6 until num>0.
  • Step5: Increment i 
  • Step 6: num=num/10
  • Step 7: Print the value of i to the user.
  • Step 8: End.

Working:

In these program we will learn to count the number of digit in a number.

Example 1 :

A simple program where we will count the number of digit.

Run
#include <stdio.h>
int main()  
{  
   int num;
   int i=0;
   printf("Enter a number");  
   scanf("%d",&num);  
   while(num!=0)  
   {  
       num=num/10;  
       i++;  
   } 
   printf("\nThe number of digits in an integer is : %d",i);  
    return 0;  
}  

Input:

Enter a number453

Output:

The number of digits in an integer is : 3

Example 2 :

Program where we will count the number of digit.

Run
#include <stdio.h>
int main()
{
    long long n;
    int i = 0;
    printf("Enter an integer: ");
    scanf("%lld", &n);
    do 
    {
        n /= 10;
        ++i;
    }while (n != 0);
  printf("Number of digits: %d", i);
}

Input:

Enter an integer: 56

Output:

Number of digits: 2

Example 3 :

Program where we will count the number of digit using a function.

Run
#include <stdio.h> 
int main()  
{  
    int num;
    int i=0;
    printf("Enter a number");  
    scanf("%d",&num);  
    i=func(num);  
    printf("Number of digits is : %d", i);  
    return 0;  
}  
int func(int n)  
{  
    int count=0;
    while(n!=0)  
    {  
        n=n/10;  
        count++;  
    }  
    return count;  
}  

Input:

Enter a number45781

Output:

Number of digits is : 5

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription