# algorithm.

Count the occurance of digits 0-9 between 1 and any number entered by the user.

Declare an array of 10 integers and initialize to 0

1. Extract each digit by using x%10. Lets digit = x%10. Now do a[digit]++;
2. Remove that number and take rest number using x/10.
3. Apply same logic till end of the digits.
4. Finally print the array with index and values. You can get no of occurrence.

Ex:
int a[10] = {};
num = 12823
digit = num % 10 ----> 3

a[digit]++ ------> a[digit] = a[digit] + 1-----> a[3] = a[3] + 1 -----> a[3] = 0 + 1 = 1.
num = num / 10; ----> 12823/10 = 1282
digit = num%10-----> 1282%10 = 2
a[digit]+±-----> a[2] = 0 + 1 = 1;
num = num/10—>1282/10 = 128
digit = num%10---->128%10---->8
a[digit]++ ----->a[8] = 0 + 1 = 1
num = num/10—>128/10=12
digit=num%10—>12%10=2
a[digit]++ = a[2] = a[2] + 1 = 1 + 1 = 2;
Follow the same way.
Finally a[0]–> No of occurrence of zeros
a[1]----> No of occurrences of 1
a[2]----> No of occurrences of 2
Like wise…

1 Like

you misunderstood the question.