Find frequency of each digit in a given integer
Sample Output
Find frequency of each digit in a given integer. Number: 192002024 Frequency of 0 = 3 Frequency of 1 = 1 Frequency of 2 = 3 Frequency of 3 = 0 Frequency of 4 = 1 Frequency of 5 = 0 Frequency of 6 = 0 Frequency of 7 = 0 Frequency of 8 = 0 Frequency of 9 = 1
Source Code
#include<stdio.h>
int main()
{
int i, digit, freqCount[10] = {0}, number, numberClone;
printf("Find frequency of each digit in a given integer.\n\nNumber: ");
scanf("%d", &number);
numberClone = number;
while(number != 0)
{
digit = (number % 10);
number = (number / 10);
freqCount[digit]++;
}
for(i = 0; i <= 9; i++)
{
/// if(freqCount[i] > 0) /** This line will be print, if frequency is greater than 0.*/
{
printf("Frequency of %d = %d\n", i, freqCount[i]);
}
}
return 0;
}
Sample Output
Find frequency of each digit in a given integer. Number: 2019966 Frequency of 0 = 1 Frequency of 1 = 1 Frequency of 2 = 1 Frequency of 3 = 0 Frequency of 4 = 0 Frequency of 5 = 0 Frequency of 6 = 2 Frequency of 7 = 0 Frequency of 8 = 0 Frequency of 9 = 2
No comments:
Post a Comment