C Programming Code Examples C > For Loops and While Loops Code Examples program to count frequency of digits in an integer program to count frequency of digits in an integer Write a C program to count frequency of digits in a given number. How to find frequency of digits in a given number using loop in C programming. Logic to find frequency of digits in a number Input a number from user. Store it in some variable say j. Declare and initialize an array of size 10 to store frequency of each digit. Why declare array of size 10? Because total number of digits is 10 i.e. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Extract last digit of given number by performing modulo division by 10. Store the result in some variable say lastDigit = j % 10. Increment the frequency of a digit found above i.e. lastDigit. To increment frequency perform freq[lastDigit]++. The value of lastDigit will be always between 0-9 inclusive. Hence it can be used as an index to freq array. Remove last digit from the number since it is processed and not required further. To remove last digit divide the number by 10 i.e. j = j / 10. Repeat step 3 to 5 till number is greater than 0. Finally print frequency of each element in the freq array. #include <stdio.h> #define BASE 10 /* Constant */ int main() { long long j, n; int i, lastDigit; int freq[BASE]; /* Input number from user */ printf("Enter any number: "); scanf("%lld", &j); /* Initialize frequency array with 0 */ for(i=0; i<BASE; i++) { freq[i] = 0; } /* Copy the value of 'j' to 'n' */ n = j; /* Run till 'n' is not equal to zero */ while(n != 0) { /* Get last digit */ lastDigit = n % 10; /* Remove last digit */ n /= 10; /* Increment frequency array */ freq[lastDigit]++; } /* Print frequency of each digit */ printf("Frequency of each digit in %lld is: \n", j); for(i=0; i<BASE; i++) { printf("Frequency of %d = %d\n", i, freq[i]); } return 0; }