ADVERTISEMENT
Check Repeated Digits in Given Input
This code takes a positive integer input, analyzes the frequency of each digit, identifies any repeated digits, and reports the repeated digits along with their frequency. It uses a while loop for digit extraction and processing, and an array to track the frequency of each digit (0-9).
#include<stdio.h>
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;
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");
}
}C Programming ExamplesOUTPUT
Enter number to find which digits are repeated
1244465288
2 --> repeated 2 times
4 --> repeated 3 times
8 --> repeated 2 times
Repeated digits are thereC Programming ExamplesExplanation:
This C program is designed to find repeated digits in each number. It analyzes the frequency of each digit within the number and reports which digits are repeated and how many times they occur. Here’s a step-by-step explanation of the code:
Header File:
- The program includes the standard C library header <stdio.h> for input and output operations.
Main Function:
- The main function is the entry point of the program.
Variable Declaration:
Several variables are declared:
- num (int): It stores the user input, which is the number to be analyzed for repeated digits.
- k (int): This variable is used to store individual digits extracted from num.
- frequency (int array): An array used to keep track of the frequency of each digit (0-9) within the number.
- flag (int): A flag to check if any digits are repeated.
- i (int): A loop counter variable.
User Input:
- The program prompts the user to enter a number:
- “Enter a number to find which digits are repeated” for num.
- The user’s input is read and stored in the variable num using scanf.
Initialization of frequency Array:
- A for loop is used to initialize the frequency array to zeros. This array will be used to store the frequency of each digit (0-9) in the input number.
While Loop:
- A while loop is used to extract and process individual digits of the number num.
- The loop continues if num is greater than 0.
Digit Extraction and Frequency Calculation:
- Within the loop, k is used to store the rightmost digit of num. This is done by calculating num % 10, which gives the remainder when num is divided by 10. This remainder is the rightmost digit.
- The frequency array is updated to increment the count for the digit k. This step calculates how many times each digit appears in the number.
- The rightmost digit is removed from num by dividing it by 10 (num = num / 10).
Checking for Repeated Digits:
- After the loop completes, a for loop is used to iterate through the frequency array to check for repeated digits.
- If frequency[i] is greater than 1 (meaning a digit appears more than once), it sets the flag to 1 and prints a message indicating which digit is repeated and how many times.
Result Reporting:
- After the loop, the program checks the flag to determine whether there are repeated digits.
- If flag is 0, it means no digits are repeated, and it prints “No Repeated Digits.”
- If flag is 1, it means repeated digits were found, and it prints “Repeated digits are there.”
End of Program:
- Once the result is displayed, the program reaches the end of the main function and terminates.