C++ Program to Find the Frequency of a Digit in a Number

frequency-of-digit

C++ programs to find the frequency of a digit in a number have been shown here. Frequency denotes the number of occurrences of an element. For example, if a number is 1102316, the frequency of the digit 1 in that number is 3.

In the following section, the algorithm, pseudocode, and time complexity of the programs have also been covered.






1. Algorithm to find the frequency of a digit in a number


1. Take a number n and a digit d as inputs.

2. Initialize a counter c = 0.

3. If n = d = 0, set c = 1 and go to step 7.

4. Perform c = c + 1, if n mod 10 = d

5. Perform n = floor(n / 10)

6. Go to step 4, if n > 0, else go to step 7

7. Declare value of c as the output.




2. Pseudocode to find the frequency of a digit in a number


Input : A number $n$ and a digit $d$

Output : Frequency of $d$ in $n$

1. Procedure countFrequency($n$, $d$):

2. $c \leftarrow 0$

3. If $n = 0$ and $d = 0$:

4. $c \leftarrow 1$

5. Else:

6. Repeat until $n = 0$

7. If $n \mod 10 = d$:

8. $c \leftarrow c + 1$

9. $n \leftarrow \lfloor{\frac{n}{10}}\rfloor$

10. Return $c$

11. End Procedure





3. Time complexity to find the frequency of a digit in a number


Time Complexity: O(log(n))

Where n is the input number.





4. Program & Output to find the frequency of a digit in a number




4.1. C++ Program & Output to find the frequency of a digit in a number Using Iteration

Code has been copied
/*******************************
       alphabetacoder.com
 C++ program to count frequency 
 of a digit in a number
********************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    int num, d, r, t, count = 0;

    // take input
    cout << "Enter the integer = ";
    cin >> num;
    cout << "Enter the digit = ";
    cin >> d;

    // copy
    t = num;

    //count the frequency of the digit
    if (num == 0 && d == 0) {
        count++;
    }
    while (num > 0) {
        // find unit digit of current number
        r = num % 10;
        // Increase the counter if unit digit = target digit
        if (r == d)
            count++;

        num = num / 10;
    }

    // display result
    cout << "Frequency of " << d << " in " << t << " = " << count << endl;

    return 0;
}

Output


Case 1:

Enter the integer = 5452755

Enter the digit = 5

Frequency of 5 in 5452755 = 4


Case 2:

Enter the integer = 996622161

Enter the digit = 6

Frequency of 6 in 996622161 = 3





4.2. C++ Program & output to find the frequency of a digit in a number using recursion

Code has been copied
/************************************
       	alphabetacoder.com
 C++ program to count frequency of a 
 digit in a number using recursion
*************************************/

#include <iostream>

using namespace std;

// recursive function to count frequency
// of a digit in an integer
int count_frequency(int num, int d) {
    // exit condition
    if (num == 0)
        return 0;
    // call the function
    return (num % 10 == d) + count_frequency(num / 10, d);
}

int main() {
    // declare variables
    int num, d, count = 0;

    // take input
    cout << "Enter the integer = ";
    cin >> num;
    cout << "Enter the digit = ";
    cin >> d;

    // if both inputs are zero
    if (num == 0 && d == 0)
        count = 1;
    else {
        //count the frequency of the digit in a 
        //number by calling recursive function
        count = count_frequency(num, d);
    }

    // display result
    cout << "Frequency of " << d << " in " << num << " = " << count << endl;

    return 0;
}

Output


Case 1:

Enter the integer = 121221

Enter the digit = 2

Frequency of 2 in 121221 = 3


Case 2:

Enter the integer = 1111

Enter the digit = 1

Frequency of 1 in 1111 = 4