 ### Program 277: Program to check whether there are repeated digits in given Number

Program 277: Program to check whether there are repeated digits in given Number

```#include<stdio.h>
main()
{
int num,k,temp,frequency,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");
}
}```
Explanation:

The Logic of Program:

We declared "frequency" to store the number of times a digit is repeated
For Ex:- frequency will store the number of times '0'  is repeated
frequency will store the number of times '1'  is repeated and so on till '9'.

So By default, we are making all the frequency of digits empty i.e assigning"frequency" array to "0" using for loop

```for(i=0;i<10;i++)
{
frequency[i]=0;
}```

Then we have to retrieve each digit and increment corresponding frequency array element.So while retrieving if the digit is '1' then we increment 'frequency'.Given below is the logic which will do this part.
```while(num>0)
{
k=num%10;
frequency[k]++;
num/=10;
}```
Explanation of above logic is:
Let's take a number '1252'

1. Since 1252>0,  it's true so the loop continues
• Now k=1252%10=2 → the last digit of the number is retrieved
• Then frequency[k]→ frequency++→ so frequency=1(*for explanation ignoring the true meaning of postfix since by now you might have understood postfix meaning)
•  num/10=1252/10→ will remove the last digit from the number and gives 125 as we have already added '1' to the corresponding frequency element.
• Finally frequency=1 and num=125 and remaining frequnecy,frequncy,...till 9 are remained with '0's.

2. Since 125>0,  it's true so the loop continues
• Now k=125%10=5 → the last digit of the number is retrieved
• Then frequency[k]→ frequency++→ so frequency=1
•  num/10=125/10→ will remove the last digit from the number and gives 12 as we have already added '1' to the corresponding frequency element.
• Finally frequency=1,frequency=1 and num=12 and remaining frequnecy,frequncy,4,6,...till 9 are remained with '0's.

3. Since 12>0,  it's true so the loop continues
• Now k=12%10=2 → the last digit of the number is retrieved
• Then frequency[k]→ frequency++→ so frequency=2
•  num/10=12/10→ will remove the last digit from the number and gives as we have already added '1' to the corresponding frequency element.
• Finally frequency=2,frequency=1 and num=1 and remaining frequnecy,frequncy,4,6,...till 9 are remained with '0's.

4. Since 1>0,  it's true so the loop continues
• Now k=1%10=1 → the last digit of the number is retrieved
• Then frequency[k]→ frequency++→ so frequency=0
•  num/10=1/10→ will remove the last digit from the number and gives 0 as we have already added '1' to the corresponding frequency element.
• Finally frequency=2,frequency=1,frequency=1 and num=0 and remaining frequncy,4,6,...till 9 are remained with '0's.

5. Since 0>0 is false loop terminates
6. So ultimate result will be frequency=2,frequency=1,frequency=1 remaining all have 0's
```for(i=0;i<10;i++)
{
if(frequency[i]>1)
{
flag=1;
printf("%d --> repeated %d times\n",i,frequency[i]);
}
}```

Using above logic we are printing only those frequency array elements that are having greater than '1'. For Ex: frequency=1 as it is not greater than '1'  it won't be printed showing that it is not a repeated digit in the given number.Similarly, frequency.
But frequency =2 since it is greater than 1 only number 2 is printed along with how many time '2' is repeated in the given number.

And Finally storing flag=1 if repeated numbers are there so that we can display a message to user that "No Repeated Digits"

Output: 