Lets write a C program to count the number of occurrences of digit k in user input positive integer number n, using recursion.
Page Contents
For Example:
If user inputs n = 12235, and k = 2, then our C program should find how many times digit 2 is present in number 12235.
Note: (num % 10) fetches last digit in num. If num = 1234; (num%10) fetches the last digit from right, which is 4.
(num/10) removes the last number from right. If num = 1234; (num/10) removes last digit i.e., 4 and the result of (num/10) will be 123.
Related Read:
C Program To Count Digit k in Number n
Video Tutorial: C Program To Count Digit k in Number n using Recursion
[youtube https://www.youtube.com/watch?v=sZhIRYXkquk]
Source Code: C Program To Count Digit k in Number n using Recursion
#include<stdio.h> int occurrence(int, int); int main() { int n, k; printf("Enter a positive integer number\n"); scanf("%d", &n); printf("Which digits occurrence you want to check for?\n"); scanf("%d", &k); printf("\n%d has appeared %d times in %d.\n", k, occurrence(n, k), n); return 0; } int occurrence(int num, int k) { if(num == 0) return 0; else if(k == (num%10)) return(1 + occurrence(num/10, k)); else return(occurrence(num/10, k)); }
Output:
Enter a positive integer number
12555
Which digits occurrence you want to check for?
5
5 has appeared 3 times in 12555.
Source Code: C Program To Count Digit k in Number n using Recursion and Ternary or Conditional Operator
#include<stdio.h> int occurrence(int, int); int main() { int n, k; printf("Enter a positive integer number\n"); scanf("%d", &n); printf("Which digits occurrence you want to check for?\n"); scanf("%d", &k); printf("\n%d has appeared %d times in %d.\n", k, occurrence(n, k), n); return 0; } int occurrence(int num, int k) { return( (num == 0)? 0 : (k == (num%10)) ? (1 + occurrence(num/10, k)) : (occurrence(num/10, k))); }
Output:
Enter a positive integer number
155555061
Which digits occurrence you want to check for?
5
5 has appeared 5 times in 155555061.
To know more about Ternary or Conditional Operator visit:
Ternary Operator / Conditional Operator In C.
Logic To Count Digit k in Number n using Recursion
First we write the base condition i.e., if num is 0, then our function returns 0 to the calling function. Else if num%10 is equal to the value of k, then we return (1 + occurrence(num/10, k)). That way we count or increment by 1 for each time we found a digit which is equal to k, and then we reduce the num by one digit from right by doing num/10. k value will remain same throughout the program execution.
If num%10 is not equal to k, then we simply divide the num by 10, and reduce the value of num by one digit from right.
Example:
Lets assume that user input value for n and k:
n = 1223; k = 2;
n | (n%10) == k | count | n/10 |
---|---|---|---|
1223 | False | 122 | |
122 | True | 1 | 12 |
12 | True | 1+1=2 | 1 |
1 | False | 1+1=2 | 0 |
So, 2 appeared 2 times in 1223.
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert