C program to count frequency of digits in an integer

Write a C program to count frequency of digits in an integer. 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 num: 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, Loop, Array

Logic to find frequency of digits in a number

Before I begin to explain the logic of counting frequency of digits in a given integer. You must have a good understanding of below three concepts -
How to find last digit of any number in C
How to reverse numbers in C
How to store and retrieve elements from an array in C

Here goes the step-by-step detailed explantion about how you gonna find the frequency of digits in an integer.
  1. First of all we need a storage where we can store frequencies of each digits. For that we will be using an integer array of size 10 call it as freq[10]. We have used an array of size 10 because decimal has base 10. There are only 10 digits that makes up any decimal number.
  2. Next, we need to initialize every element of the array with 0. Assuming that every digit has occurred 0 times.

  3. Now comes the main logic. Find the last digit of the given number. For that we need to perform modular division by 10 i.e. lastDigit = num % 10 (where num is the number whose frequency of digits is to be found).
  4. Increment the freq[ lastDigit ]++ by one.
  5. Now remove the last digit from the num as it isn't needed anymore. Perform num = num / 10.
  6. Repeat steps 3-5 till num != 0. Finally you will be left with an array freq having the frequency of each digits in that number.
Lets, now implement this on code.

Program to count frequency of digits in an integer

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

#include <stdio.h>
#define BASE 10

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

    printf("Enter any number: ");
    scanf("%lld", &num);

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

    n = num; //Copies the value of num to n

    while(n != 0)
    {
        // Gets the last digit
        lastDigit = n % 10;

        // Increments the frequency array
        freq[lastDigit]++;

        // Removes the last digit from n
        n /= 10;
    }

    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;
}


Do not confuse with n /= 10. It is same as n = n / 10.


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 ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment