C program to find frequency of each digit in a given number
Learn how to write a C program to find the frequency of each digit in a given number. This article provides a detailed explanation and sample code for this fundamental task in C programming.
Finding the frequency of each digit in a given number is a common problem in programming that helps in understanding arrays, loops, and basic arithmetic operations. This article will guide you through writing a C program to determine the frequency of each digit (0-9) in a given number, providing a detailed explanation and sample code.
Steps to Find the Frequency of Each Digit
To solve this problem, follow these steps:
- Input the Number: Read the number from the user.
- Initialize an Array: Use an array to store the frequency of each digit.
- Extract Each Digit: Use a loop to extract each digit of the number.
- Update the Frequency: Increment the corresponding index in the array.
- Print the Result: Output the frequency of each digit.
Write a C program to find frequency of each digit in a given number
Here's a C program to find the frequency of each digit in a given number:
#include <stdio.h>
int main() {
int number, digit;
int frequency[10] = {0}; // Initialize the frequency array with zeros
// Input the number
printf("Enter a number: ");
scanf("%d", &number);
// Handle negative numbers
if (number < 0) {
number = -number;
}
// Calculate the frequency of each digit
while (number != 0) {
digit = number % 10; // Extract the last digit
frequency[digit]++; // Increment the corresponding frequency
number /= 10; // Remove the last digit
}
// Print the frequency of each digit
printf("Digit Frequency\n");
for (int i = 0; i < 10; i++) {
if (frequency[i] != 0) {
printf("%d %d\n", i, frequency[i]);
}
}
return 0;
}
Output
Enter a number: 123432
Digit Frequency
1 1
2 2
3 2
4 1
Explanation
- Input the Number: The program prompts the user to enter an integer. The
scanffunction reads the input number and stores it in the variablenumber. - Initialize an Array: An array
frequencyof size 10 is initialized to store the frequency of each digit. The indices of the array correspond to the digits 0 through 9, and the values at these indices represent the frequency of the corresponding digit. - Handle Negative Numbers: If the input number is negative, it is converted to a positive number for simplicity.
- Extract Each Digit:
- The
whileloop continues as long asnumberis not equal to0. - The last digit is extracted using the modulus operator (
%). For example, ifnumberis12321,digit = 12321 % 10will result indigit = 1.
- The
- Update the Frequency:
- The extracted digit is used as an index to the
frequencyarray, and the corresponding value is incremented. For example, ifdigit = 1,frequency[1]++increments the count of the digit1by 1. - The last digit is removed from
numberusing integer division (/). For example,number = 12321 / 10results innumber = 1232.
- The extracted digit is used as an index to the
- Print the Result:
- A
forloop iterates over thefrequencyarray and prints the frequency of each digit that appears in the input number.
- A
Detailed Steps
- Step 1: Input the Number
- Use
printfto prompt the user for input. - Use
scanfto read the number from the user.
- Use
- Step 2: Initialize an Array
- Declare an array
frequencyof size 10 and initialize all elements to 0.
- Declare an array
- Step 3: Handle Negative Numbers
- Convert the number to positive if it is negative.
- Step 4: Extract Each Digit
- Use a
whileloop to continue processing as long asnumberis not zero. - Use the modulus operator (
%) to get the last digit of the number.
- Use a
- Step 5: Update the Frequency
- Increment the value at the index corresponding to the extracted digit in the
frequencyarray. - Remove the last digit from the number using integer division (
/).
- Increment the value at the index corresponding to the extracted digit in the
- Step 6: Print the Result
- Use a
forloop to iterate over thefrequencyarray. - Print the digit and its frequency if the frequency is not zero.
- Use a
Example
For an input of 12321, the program will:
- Extract
1and incrementfrequency[1](frequency[1] = 1). - Extract
2and incrementfrequency[2](frequency[2] = 1). - Extract
3and incrementfrequency[3](frequency[3] = 1). - Extract
2and incrementfrequency[2](frequency[2] = 2). - Extract
1and incrementfrequency[1](frequency[1] = 2).