C program to find frequency of each digit in a given number

Category: C Program

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:

  1. Input the Number: Read the number from the user.
  2. Initialize an Array: Use an array to store the frequency of each digit.
  3. Extract Each Digit: Use a loop to extract each digit of the number.
  4. Update the Frequency: Increment the corresponding index in the array.
  5. 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

  1. Input the Number: The program prompts the user to enter an integer. The scanf function reads the input number and stores it in the variable number.
  2. Initialize an Array: An array frequency of 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.
  3. Handle Negative Numbers: If the input number is negative, it is converted to a positive number for simplicity.
  4. Extract Each Digit:
    • The while loop continues as long as number is not equal to 0.
    • The last digit is extracted using the modulus operator (%). For example, if number is 12321, digit = 12321 % 10 will result in digit = 1.
  5. Update the Frequency:
    • The extracted digit is used as an index to the frequency array, and the corresponding value is incremented. For example, if digit = 1, frequency[1]++ increments the count of the digit 1 by 1.
    • The last digit is removed from number using integer division (/). For example, number = 12321 / 10 results in number = 1232.
  6. Print the Result:
    • A for loop iterates over the frequency array and prints the frequency of each digit that appears in the input number.

Detailed Steps

  • Step 1: Input the Number
    • Use printf to prompt the user for input.
    • Use scanf to read the number from the user.
  • Step 2: Initialize an Array
    • Declare an array frequency of size 10 and initialize all elements to 0.
  • Step 3: Handle Negative Numbers
    • Convert the number to positive if it is negative.
  • Step 4: Extract Each Digit
    • Use a while loop to continue processing as long as number is not zero.
    • Use the modulus operator (%) to get the last digit of the number.
  • Step 5: Update the Frequency
    • Increment the value at the index corresponding to the extracted digit in the frequency array.
    • Remove the last digit from the number using integer division (/).
  • Step 6: Print the Result
    • Use a for loop to iterate over the frequency array.
    • Print the digit and its frequency if the frequency is not zero.

Example

For an input of 12321, the program will:

  • Extract 1 and increment frequency[1] (frequency[1] = 1).
  • Extract 2 and increment frequency[2] (frequency[2] = 1).
  • Extract 3 and increment frequency[3] (frequency[3] = 1).
  • Extract 2 and increment frequency[2] (frequency[2] = 2).
  • Extract 1 and increment frequency[1] (frequency[1] = 2).

Recommended Posts