C 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 total occurrences of each digits in a given number in C program.

Example

Input

Input any number: 116540

Output

Frequency of 0 = 1 
Frequency of 1 = 2 
Frequency of 2 = 0 
Frequency of 3 = 0 
Frequency of 4 = 1 
Frequency of 5 = 1 
Frequency of 6 = 1 
Frequency of 7 = 0 
Frequency of 8 = 0 
Frequency of 9 = 0

Required knowledge

Basic C programming, For loop, While loop, Array

Logic to find frequency of digits in a number

Step by step description to count frequency of digits in a number.

  1. Input a number from user. Store it in some variable say num.
  2. 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.
  3. Extract last digit of given number by performing modulo division by 10. Store the result in some variable say lastDigit = num % 10.
  4. 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.

  5. 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. num = num / 10.
  6. Repeat step 3 to 5 till number is greater than 0.
  7. Finally print frequency of each element in the freq array.

Program to count frequency of digits in a number

/**
 * C program to count frequency of digits in a given number
 */

#include <stdio.h>
#define BASE 10 /* Constant */

int main()
{
    long long num, n;
    int i, lastDigit;
    int freq[BASE];

    /* Input number from user */
    printf("Enter any number: ");
    scanf("%lld", &num);

    /* Initialize frequency array with 0 */
    for(i=0; i<BASE; i++)
    {
        freq[i] = 0;
    }

    /* Copy the value of 'num' to 'n' */
    n = num; 

    /* 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", num);
    for(i=0; i<BASE; i++)
    {
        printf("Frequency of %d = %d\n", i, freq[i]);
    }

    return 0;
}

Let us take a note on above program.

  • #define BASE 10 defines a constant with name BASE. Since there are 10 digits in decimal number system hence I have defined it as a constant variable.
  • I have declared num with long long data type and to access a long long I have used %lld format specifier.

    However some compiler does not support long long type. If you are facing issues with long long type you can use long or int type.

  • In statement n /= 10; I have used shorthand assignment operator which is equivalent to n = n / 10;.
  • The increment statement freq[lastDigit]++; is equivalent to freq[lastDigit] = freq[lastDigit] + 1;.

Output

Enter any number: 11203458760011
Frequency of each digit in 11203458760011 is:
Frequency of 0 = 3
Frequency of 1 = 4
Frequency of 2 = 1
Frequency of 3 = 1
Frequency of 4 = 1
Frequency of 5 = 1
Frequency of 6 = 1
Frequency of 7 = 1
Frequency of 8 = 1
Frequency of 9 = 0

Happy coding 😉