In: Computer Science
this is a C++ question.
1 Write a program that returns the count of the
occurences of 8 as a digit within an integer.The input to the
program should be an integer, not a string.For example 848 yields 2
.You are expected to solve this using a recursive function (no
loops)
Hint:Note that mod (%) by 10 yields the rightmost digit (787 % 10
is 7 ) while division by 10 removes the rightmost digit (787/10 is
78)
#include <iostream>
using namespace std;
//Declaring global variables
int count=0,rem;
//Function declaration
void countNum8(int number);
int main()
{
//Declaring variable
int number;
//Getting thennumber entered by the user
cout<<"Enter a number :";
cin>>number;
//calling the function by passing the user input as argument
countNum8(number);
//Displaying the result
cout<<"The number of 8's present in the
"<<number<<" is :"<<count<<endl;
return 0;
}
/* Function implementation which counts the
* number of occurrences of digit 8 in the number
*/
void countNum8(int number)
{
if(number>0)
{
rem=number%10;
if(rem==8)
{
count++;
}
countNum8(number/10);
}
}
____________________________
output:

______________Thank You