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 202223, the frequency of the digit 2 in that number is 4.
In the following section, the algorithm, pseudocode, and time complexity of the programs have also been covered.
Page content(s):
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.
3.
4.
5.
6.
7.
8.
9.
10.
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
/***************************** alphabetacoder.com C program to count frequency of a digit in a number ******************************/ #include <stdio.h> int main() { // declare variables int num, d, r, t, count = 0; // take input printf("Enter the integer = "); scanf("%d", & num); printf("Enter the digit = "); scanf("%d", & 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 printf("Frequency of %d in %d = %d", d, t, count); return 0; }
Output
Case 1:
Enter the integer = 177007
Enter the digit = 7
Frequency of 7 in 177007 = 3
Case 2:
Enter the integer = 0
Enter the digit = 0
Frequency of 0 in 0 = 1
4.2. C Program & output to find the frequency of a digit in a number using recursion
/********************************** alphabetacoder.com C program to count frequency of a digit in a number using recursion ***********************************/ #include <stdio.h> // 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 printf("Enter the integer = "); scanf("%d", & num); printf("Enter the digit = "); scanf("%d", & 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 printf("Frequency of %d in %d = %d", d, num, count); return 0; }
Output
Case 1:
Enter the integer = 121221
Enter the digit = 2
Frequency of 2 in 121221 = 3
Case 2:
Enter the integer = 5555
Enter the digit = 4
Frequency of 4 in 5555 = 0