Coding Practice

Write a C program to find frequency of each digit in a given integer

Find frequency of each digit in a given integer
Sample Output
Find frequency of each digit in a given integer.

Number: 883499
Frequency of 0 = 0
Frequency of 1 = 0
Frequency of 2 = 0
Frequency of 3 = 1
Frequency of 4 = 1
Frequency of 5 = 0
Frequency of 6 = 0
Frequency of 7 = 0
Frequency of 8 = 2
Frequency of 9 = 2
Source Code
#include<stdio.h>

int main()
{
    int i, count, digit, number, numberClone;

    printf("Find frequency of each digit in a given integer.\n\nNumber: ");
    scanf("%d", &number);

    numberClone = number;

    for(i = 0; i <= 9; i++)
    {
        count = 0;
        number = numberClone;

        while(number != 0)
        {
            digit = (number % 10);
            number = (number / 10);
            if(digit == i)
            {
                count++;
            }
        }
        /// if(count > 0) /** This line will be print, if frequency is greater than 0.*/
        {
            printf("Frequency of %d = %d\n", i, count);
        }
    }
    return 0;
}
Sample Output
Find frequency of each digit in a given integer.

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

No comments:

Post a Comment

Change Theme
X