Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a recursive function int count_digit(int digit, int numb) that returns the

ID: 3857761 • Letter: W

Question

Write a recursive function

int count_digit(int digit, int numb)

that returns the number of occurrences of digit in the decimal representation of numb. digit is an int between zero and nine inclusive. For example, the call count_digit(8, 18488) should return 3, because there are three eights in the decimal representation of 18488. Recall that if num is an integer, then num/10 is the integer part of num divided by ten, while num%10 is the remainder of num divided by ten. These provide a way to split the problem into smaller pieces. All looping in your function must be accomplished with recursion---you may not use any of iteration constructs. You may use a helper function if you like, but none is necessary.

Explanation / Answer

#include <stdio.h>

//function to count digits
int countDigits(int digit,int numb)
{
static int count=0;

if(numb>0)
{


int rem=numb%10;
if(rem==digit)
count++;
numb=numb/10;
countDigits(digit,numb);
}
else
{
return count;
}
}
int main()
{
  
int count=0;



count=countDigits(8,8388);

printf("Total 8 in 8388 is: %d ",count);

return 0;
}

========================================

Output:

akshay@akshay-Inspiron-3537:~/Chegg$ gcc countrecu.c
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Total 8 in 8388 is: 3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote