Question

In: Computer Science

this is a C++ question. 1 Write a program that returns the count of the occurences...

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)

Solutions

Expert Solution

#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


Related Solutions

In C++ For this assignment, you will write a program to count the number of times...
In C++ For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array...
Write a C program to find the count of words and optionally the longest and or...
Write a C program to find the count of words and optionally the longest and or shortest words in a string input by the user or coming from a file. If there is no filename the user would enter the string right after running the command. You must use getopt to parse the command line. Usage: words [-l] [-s] [filename] The l flag means to find the longest and the s option means to find the shortest. You may have...
C LANGUAGE ONLY Write a C program to count the frequency of each element in an...
C LANGUAGE ONLY Write a C program to count the frequency of each element in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements of the array: element [0]: 25 element [1]: 12 element [2]: 43 Expected output: The frequency of all elements of an array: 25 occurs 1 times 12 occurs 1 times 3 occurs 1 times
Java Question 5: Count elements in the heap Write a function that returns the number of...
Java Question 5: Count elements in the heap Write a function that returns the number of elements in a min heap strictly less than a given number. Method signature: public static int elemNumHeap(PriorityQueue minHeap, int val) Please also include testers.
Problem: Write a C/C++ program to implement a function that returns 1 when an integer x...
Problem: Write a C/C++ program to implement a function that returns 1 when an integer x contains an odd number of 1s (in its binary representation) and 0 otherwise. You can consider x as a four byte integer, i.e. w = 32 bits. Constraint: Your solution can use at most 12 arithmetic, bitwise, and logical operations.
In the language c using the isspace() function: Write a program to count the number of...
In the language c using the isspace() function: Write a program to count the number of words, lines, and characters when user enter statements as input. A word is any sequence of non-white-space characters. Have the program continue until end-of-file. Make sure that your program works for the case of several white space characters in a row. The character count should also include white space characters. Example of the user input could be the statement below:              You're traveling through ​              ...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements in the arrangement: element [0]: 5 element [1]: 1 element [2]: 1 Expected output: The total number of duplicate elements found in the array is: 1
in basic c++ program please!! Write a word search and word count program. Assign the following...
in basic c++ program please!! Write a word search and word count program. Assign the following text to a string constant. For God so loved the world that he gave his one and only Son, that whoever believes in him shall not perish but have eternal life. For God did not send his Son into the world to condemn the world, but to save the world through him.Whoever believes in him is not condemned, but whoever does not believe stands...
Write a c++ program that prints the count of all prime numbers between A and B...
Write a c++ program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = Any 5 digit unique number B = A + 1000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
2.c++ if and loop statement Write a program that will count the number of even number...
2.c++ if and loop statement Write a program that will count the number of even number and odd numbers between two inputted numbers. Display the numbers and compute the sum and average of all the even numbers and the sum and average all the odd numbers. Sample outputs: Enter starting number:3 Enter starting number:4 Enter ending number:10 Enter ending number:10 odd numbers Even number 3 4 5 6 7 8 9 10 number of even numbers=4 number of even numbers=4...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT