C program to count frequency of digits in an integer

Previous Program Next Program

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, Loop, Array

Logic to find frequency of digits in any number

Before I explain the logic of counting frequency of digits. You must have a good understanding of below three concepts.

Below is the step by step description to count frequency of digits in an integer.

  1. Read a number from user. Store it in some variable say num.
  2. Define an array to hold frequency of size 10 i.e. freq[10]. Now why size 10? Because total number of digits is 10 0, 1, 2, ... , 8, 9.
  3. Initialize all elements of frequency array with 0 i.e. freq[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}. Assuming that the 0th index will hold the frequency of 0. 1st index will hold frequency of 1. So on 9th index of freq will store the frequency of 9.
  4. Extract the last digit of number by performing modulo division by 10. Store the last digit in some variable say lastDigit = num % 10. The value in lastDigit is always between 0-9 inclusive. This last digit can be used as an index to freq array.
  5. Remove the last digit of the number. As it is processed and not required further. Last digit is removed by dividing the number by 10 i.e. num = num / 10.
  6. Increment the frequency value of lastDigit found above. Using freq[lastDigit]++.
  7. Repeat step 4-6 till number is greater than 0.

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

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

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

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

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

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

        // Remove the last digit
        n /= 10;

        // Increment the 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;
}

Note: Do not confuse with the statement n /= 10. It is same as n = n / 10. You can use any of them according to your comfort.

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

You may also like

Previous Program Next Program
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
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

No comments:

Post a Comment