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 ******************************/ using System; namespace DigitFrequency { class Program { static void Main(string[] args) { // declare variables int num, d, r, t, count = 0; // take input Console.Write("Enter the integer = "); num = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter the digit = "); d = Convert.ToInt32(Console.ReadLine()); // 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 Console.WriteLine("Frequency of " + d + " in " + t + " = " + count); // wait for user to press any key Console.ReadKey(); } } }
Output
Enter the integer = 7705787
Enter the digit = 7
Frequency of 7 in 7705787 = 4
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 ************************************/ using System; namespace DigitFrequency { class Program { // recursive function to count frequency // of a digit in an integer static int count_frequency(int num, int d) { // exit condition if (num == 0) return 0; // call the function return ((num % 10 == d ? 1 : 0) + count_frequency(num / 10, d)); } static void Main(string[] args) { // declare variables int num, d, count = 0; // take input Console.Write("Enter the integer = "); num = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter the digit = "); d = Convert.ToInt32(Console.ReadLine()); // 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 Console.WriteLine("Frequency of " + d + " in " + num + " = " + count); // wait for user to press any key Console.ReadKey(); } } }
Output
Enter the integer = 6855663
Enter the digit = 6
Frequency of 6 in 6855663 = 3