C Program to Print the Frequency of digits of a given number

Write a program to print the frequency of digits in a given number

Input Format
The input contains an integer 

Output Format
Print the required output

Constraint
 1<=input<=10^12Sample Input 1

120387332

Sample Output 1

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

Code:

#include <stdio.h>
#define BASE 10 

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

    scanf("%lld", &num);

    for(i=0; i<BASE; i++)
    {
        freq[i] = 0;
    }

    n = num; 

    while(n != 0)
    {
        lastDigit = n % 10;
        n /= 10;
        freq[lastDigit]++;
    }

    for(i=0; i<BASE; i++)
    {
        printf("Frequency of %d = %d\n", i, freq[i]);
    }

    return 0;
}
Code language: C/AL (cal)

Leave a Comment