# c program to find frequency of digits in a number

c program to find frequency of digits in a number  is a simplest program in c mostly such type of program requires only basic knowledge.

In this tutorial we are going to learn c program to find frequency of digits in a number with example.

This program checks the frequency of each digits in given number.

Logic you require to find  frequency of digits in a number :

We take a number from user. After that we separate out each digits one by one from a given number and increment frequency for that particular digits simultaneously. At last week print all frequency of each digits in given number.

Example :-

Num = 112333

1 present 2 times

2 present 1 times

3 present 3 times.

Concept you require to know from c :

1. Basic input output

3. While and for loop in c

Algorithm to check frequency of digits in given number:

1. Start

2. Declare variables

3. Take a number input from user

4. Set frequency of each digital from 0 to 10 to start i.e. 0

5. Traverse the given number from end to start using loop

6. Separate each digit from number and increment frequency for that digit.

7. After traversing all digits stop

8. Now print frequency for each digits from 0 to 10.

9. Stop

### C program to find the frequency of occurrence of digit in the given number

Output :

Explanation :

2. Write a main function from where program execution starts.

3. Now declare the variables

num => to store a given number

temp and i=> is temporary variable

frequency => to store frequency of each digit

flag => is used to set flag

4. Take a number from user

Suppose for example : num = 11233

5. Copy given number to temp.temp = 11233

6. Set frequency for each digits from 0 to 9 to start with default value as 0. For this we use for loop as for(i=0;i<10;i++)

for loop iteration :

i = 0 , 0 < 10 this condition becomes true so loop executed

frequency[i]=0  => frequency = 0

This process continues till for loop encounters i value to 10.

7. Next week need to separate digits in given number and check for repeated number and store count for each digits. So we write while loop which is used to traverse a given number from end to start.

While loop iteration 1:

while(num>0) => 11233 > 0 this condition becomes true and loop executed

k=num%10 => k = 3

frequency[k]++ => frequency=1

num/=10 => 1123

While loop iteration 2:

while(num>0) => 1123 > 0 this condition becomes true and loop executed

k=num%10 => k = 3

frequency[k]++ => frequency=2

num/=10 => 112

While loop iteration 3:

while(num>0) => 112 > 0 this condition becomes true and loop executed

k=num%10 => k = 2

frequency[k]++ => frequency=1

num/=10 => 11

While loop iteration 4:

while(num>0) => 11 > 0 this condition becomes true and loop executed

k=num%10 => k = 1

frequency[k]++ => frequency=1

num/=10 => 1

While loop iteration 5:

while(num>0) => 1 > 0 this condition becomes true and loop executed

k=num%10 => k = 1

frequency[k]++ => frequency=2

num/=10 => 0

While loop iteration 6:

while(num>0) => 0 > 0 this condition becomes false and execution come out of the loop.

8. Now we have a frequency of each digits in given number. We need to check and print it.

So we use for loop to check digits from 0 to 9

for(i=0;i<10;i++)

For loop iteration 1 :

i = 0 , 0 < 10 this condition becomes true and for loop executed

if(frequency[i]>1)  => frequency > 1 this condition becomes false and if block will not executed and i incremented by 1.

For loop iteration 2 :

i = 1 , 1 < 10 this condition become true and loop executed

if(frequency[i]>1)  => frequency > 1 this condition becomes true and of block executed.

flag=1;

printf(“%d –> repeated %d times\n”,i,frequency[i]) => 1 is repeated 2 times

For loop iteration 3 :

i = 2 , 2 < 10 this condition becomes true and for loop executed

if(frequency[i]>1)  => frequency > 1 this condition becomes false and if block will not executed and i incremented by 1.

9. This process continues till i = 10

10. Now we printed all repeated digits in given number.

11. If given number does not contain any repeated digits in number then we print this by checking flag value.

if(flag==0)

printf(“No Repeated Digits\n”);

1. End with the program.