Home » Count the Number of Digits in an Integer using the while loop

Count the Number of Digits in an Integer using the while loop

Learn about Count the Number of Digits in an Integer using the while loop in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Contents

Count the Number of Digits in an Integer using the while loop

Program:

 #include<stdio.h>
 int main()
 {
     int number, count=0;

     printf("Enter a number: ");
     scanf("%d",&number);

     while (number!=0)
     {
       count++;
       number /= 10;
     }

     printf("Number of digits =  %d\n",count);

     return 0;
 }

Output:

Enter a number: 3345
Number of digits = 4

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
Factorial program in C using do while loop
Factorial program in C using while loop