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

Write one single complete C++ program to do all two sections below: Part A: Recu

ID: 3822886 • Letter: W

Question

Write one single complete C++ program to do all two sections below: Part A: Recursion (from professor Yang) Write a recursive function called countFive. It counts how many 5's appears in a given number passed in as a parameter. SAMPLE RUN::: 515054 counting digit 5: 3 177771 counting digit 5: 0 5 counting digit 5: 1 Write a recursive function called printStarDigit which prints out all the digit in the given number passed in as a parameter. Also, it prints out a * at the beginning and end of this number and in between each digit. Ex: Number: 23455212 *2*3*4*5*5*2*1*2* Number: 7 *7* Number: 18 *1*8*

Explanation / Answer

#include <iostream>
using namespace std;

//Function to count the number of 5s in a number
int countFive(int n)
{
if(n==0)
return 0; //Base case when n=0
if(n%10 == 5)
return 1 + countFive(n/10); //If last digit is 5, increment count and remove last digit
else
return countFive(n/10); //Else remove the last digit
}

//Function to print all digits separated by stars
void printStarDigit (int n)
{
if(n<10)
cout << "*" << n << "*"; //Base case when only one digit is left
else
{
printStarDigit(n/10); //Recursive call to the function
cout << n%10 << "*";
}
}

//Driver function main() to test to above function
int main()
{
int n;
cout << "Number: ";
cin >> n;

cout << " Counting Digit 5: ";
cout << countFive(n);

cout << " ";
printStarDigit(n);
}

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