Computer Science, asked by puttarajaputta, 2 months ago

write a program to find frequency of digit in a number in c++program and in while loop​

Answers

Answered by karthik22245
1

Answer:

Step by step description to count frequency of digits in a number.

Input a number from user. Store it in some variable say num.

Declare and initialize an array of size 10 to store frequency of each digit. Why declare array of size 10? Because total number of digits is 10 i.e. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.

Extract last digit of given number by performing modulo division by 10. Store the result in some variable say lastDigit = num % 10.

Increment the frequency of a digit found above i.e. lastDigit. To increment frequency perform freq[lastDigit]++.

The value of lastDigit will be always between 0-9 inclusive. Hence it can be used as an index to freq array.

Remove last digit from the number since it is processed and not required further. To remove last digit divide the number by 10 i.e. num = num / 10.

Repeat step 3 to 5 till number is greater than 0.

Finally print frequency of each element in the freq array.

Similar questions