Question

Use C++ to write a recursive function that takes in a non-negative integer and returns the...

Use C++ to write a recursive function that takes in a non-negative integer and returns the count of the occurrences of 7 as a digit, so for example 717 yields 2. Note: Even though it would be easy to solve this problem iteratively, the goal is to get some practice solving problems recursively.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

int count7(int num) {
    if (num <= 0) {
        return 0;
    } else {
        int count = count7(num/10);
        if (num % 10 == 7) {
            count++;
        }
        return count;
    }
}

int main() {
    cout << count7(717) << endl;
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
Use C++ to write a recursive function that takes in a non-negative integer and returns the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write c++ recursive function 3. A function that takes an integer, and returns the product of...

    Write c++ recursive function 3. A function that takes an integer, and returns the product of the digits, so the input 1989 would return 1∗9∗8∗9 = 648 4. A function that does the above repeatedly, taking a number and computing the product of it’s digits until you get a single digit number: so on input 1989 it would go 1989→648→192→18→8 and return 8. (Note, you can use the previous functions if you want.

  • need help with python ( no loops used please!) Write a recursive function named sum_digits that...

    need help with python ( no loops used please!) Write a recursive function named sum_digits that takes a given a non-negative integer N and returns the sum of its digits. Hint: Mod (%) by 10 gives you the rightmost digit (126 % 10 is 6), while doing integer division by 10 removes the rightmost digit (126/10 is 12). This function has to be recursive; you are not allowed to use loops to solve this problem! This function takes in one...

  • Write a recursive function sum-odds that takes a non-empty list of integers

    in python Part I: Sum of Odd Integers Write a recursive function sum-odds that takes a non-empty list of integers as an argument and returns the sum of only the odd integers in the list. In class we explored a recursive function called rsum that recursively computes the sum of a list of integers use it as a model to get started. Your function must be recursive and must not use any loops

  • ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and...

    ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and including the given value. This function has to be recursive; you may not use loops! For example: Test Result print('%d : %d' % (5, sum_up_to(5))) 5 : 15 Q2, Write a recursive function that counts the number of odd integers in a given list. This function has to be recursive; you may not use loops! For example: Test Result print('%s : %d' % ([2,...

  • 1. DOES A DIGIT APPEAR IN AN INTEGER, Write a recursive function appears(n,i) that returns true...

    1. DOES A DIGIT APPEAR IN AN INTEGER, Write a recursive function appears(n,i) that returns true if the digit i appears in the positive (base 10) integer n, false if it does not in c++. ex. Enter a positive integer: 54321 Enter a digit: 7 The digit 7 does NOT appear in the integer 54321. 2. IS AN INPUT STRING A PALINDROME? A palindrome is a string in which the letters are the same in both directions, disregarding capitalization and...

  • Write a recursive function named arithmeticSum that takes a positive integer parameter n and returns the...

    Write a recursive function named arithmeticSum that takes a positive integer parameter n and returns the sum of the integer numbers from 1 to n Please write in C++

  • Write a recursive fucntion aNbN that takes an integer and returns the Stringaa...abb...b for example: aNbN...

    Write a recursive fucntion aNbN that takes an integer and returns the Stringaa...abb...b for example: aNbN (0) returns aNbN (1) returns ab aNbN (3) returns aaabbb Updated 35 mins ago: using format #include <iostream> #include <string> using namespace std;

  • Using Java IDE Write a recursive method which takes an integer number and returns the sum...

    Using Java IDE Write a recursive method which takes an integer number and returns the sum of the numbers from 1 to that number. The method must solve the problem recursively.Then write an application which calls the method with a few different numbers and displays the return value of the method.

  • (Place your answer to this problem in the “count_largest.py” file) Complete the count_largest() function, which takes...

    (Place your answer to this problem in the “count_largest.py” file) Complete the count_largest() function, which takes a single positive (non-zero) integer as its argument. This function identifies the largest digit in the argument/parameter and returns an integer value that represents the total number of occurrences of that digit. For example, the largest digit in 4234 is 4, and it appears twice. Thereareseveralwaystosolvethisproblem,mostofwhichinvolvePythonfunctionsorconceptsthatwehaven’tcoveredin class yet, but it’s definitely possible to solve it using only what we know right now. In particular,...

  • Python Please..  Write the function kthDigit(n, k) that takes a possibly-negative int n and a non-negative int...

    Python Please..  Write the function kthDigit(n, k) that takes a possibly-negative int n and a non-negative int k, and returns the kth digit of n, starting from 0, counting from the right. So kthDigit(789, 0) returns 9, kthDigit(789, 2) returns 7, kthDigit(789, 3) returns 0, and kthDigit(-789, 0) returns 9.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT