C Programming Code Examples C > For Loops and While Loops Code Examples C program to count number of digits in an integer C program to count number of digits in an integer Write a C program to input a number from user and count number of digits in the given integer using loop. How to find total digits in a given integer using loop in C programming. Logic to count number of digits in an integer First logic is the easiest and is the common to think. It uses loop to count number of digits. To count number of digits divide the given number by 10 till number is greater than 0. For each iteration increment the value of some count variable. Input a number from user. Store it in some variable say j. Initialize another variable to store total digits say digit = 0. If j > 0 then increment count by 1 i.e. count++. Divide j by 10 to remove last digit of the given number i.e. j = j / 10. Repeat step 3 to 4 till j > 0 or j != 0. #include <stdio.h> int main() { long long j; int count = 0; /* Input number from user */ printf("Enter any number: "); scanf("%lld", &j); /* Run loop till j is greater than 0 */ while(j != 0) { /* Increment digit count */ count++; /* Remove last digit of 'j' */ j /= 10; } printf("Total digits: %d", count); return 0; }