View All C Programs

 Previews Example  View All C Programs Next Example 

Program to Count Number of Digits of an Integer in C

This program takes an integer from user and calculates the number of digits in that integer.
 #include <stdio.h>
int main()
{
  int n,count=0;
  printf("Enter an integer: ");
  scanf("%d", &n);
  while(n!=0)
  {
      n=n/10          
      ++count;
  }
  printf("Number of digits: %d",count);
}
Result
Enter an integer: 9478
Number of digits: 4