C program to find frequency of digits in a number is the simplest program checks the frequency of each digit in a given number.

Example :-
Num = 112333
1 present 2 times
2 present 1 times
3 present 3 times.

Algorithm to check frequency of digits in given number

  1. Declare variables
  2. Take number of input from a user
  3. Set frequency of each digital from 0 to 10 to start i.e. 0
  4. Traverse the given number from end to start using the loop
    Separate each digit from the number
    If number repeated
    Increment frequency for that digit.
  5. Print frequency for each digit from 0 to 10.

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

#include<stdio.h>

void main()

{

int num,k,temp,frequency[9],flag=0,i;

printf("Enter number to find which digits are repeated\n");

scanf("%d",&num);

temp=num;

//By default setting frequency of digits as zero

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

{

frequency[i]=0;

}
while(num>0)

{

k=num%10;

frequency[k]++;

num/=10;

}

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

{
if(frequency[i]>=1)
{
flag=1;

printf("%d --> repeated %d times\n",i,frequency[i]);

}

}

if(flag==0)

{

printf("No Repeated Digits\n");

}

else

{

printf("Repeated digits are there\n");

}

}

Output :

C program to find frequency of digits in a number

Write A Comment