Question

C++: Write a recursive function that does the following: Given a number, add all the digits...

C++: Write a recursive function that does the following:

Given a number, add all the digits and display the sum.

Example:

                        The sum of the number 5432 would be 14.

PLEASE PAY ATTENTION TO THE FOLLOWING:

    • Do not use the static modifier. No global variables. Your program should implement a non-tail recursive algorithm. In other words, it should do something as it moves towards the base case, the tail, and also do something as it comes back from the tail to the beginning.
    • The input is going to be received as a single integer from the user, in the main function. The input will not be more than four digits long (no validation necessary).
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include<iostream>
using namespace std;

int getSum(int n){
   if(n < 10){
      return n;
   }
   else{
      return (n%10) + getSum(n/10);
   }
}

int main() {
   int n;
   cout<<"Enter number: ";
   cin>>n;
   cout<<"The sum of the digits is: "<<getSum(n)<<endl;
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++: Write a recursive function that does the following: Given a number, add all the digits...
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
  • C++: Write a recursive function that does the following: Given a number that could be up...

    C++: Write a recursive function that does the following: Given a number that could be up to 10 digits long, place the commas in the appropriate places. PLEASE PAY ATTENTION TO THE FOLLOWING: - Do not use the static modifier. No global variables. - The input has to be in number format. For example the number 1087045 would be displayed as 1,087,045

  • Write a recursive function in Python to find the sum of digits of a number. Name...

    Write a recursive function in Python to find the sum of digits of a number. Name the function sum_of_digits. The function should use recursive algorithm (calling itself). The function should print out the sum of all the digits of a given number. For example, sum_of_digits(343) should have a output of 10. Marks will be deducted if you do not follow strictly to the instructions. [3]: N 1 def sum_of_digits(n): HNM in sum_of_digits (343) Out[3]: 10

  • PYTHON: (Sum the digits in an integer using recursion) Write a recursive function that computes the...

    PYTHON: (Sum the digits in an integer using recursion) Write a recursive function that computes the sum of the digits in an integer. Use the following function header: def sumDigits(n): For example, sumDigits(234) returns 9. Write a test program that prompts the user to enter an integer and displays the sum of its digits. Sample Run Enter an integer: 231498 The sum of digits in 231498 is 27

  • In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

    In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....

  • Scheme number computer a. Write a recursive procedure (digits n) that computes the number of digits...

    Scheme number computer a. Write a recursive procedure (digits n) that computes the number of digits in the integer n using a recursive process. For example, (digits 42) should return 2 and (digits 13579) should return 5. You may make use of the built in floor predicate for truncating decimals to whole numbers. b. Rewrite your procedure from part (a) using an iterative process. Call the function (digits-iter n).

  • With your partner, brainstorm a program that will ask the user for a number of digits...

    With your partner, brainstorm a program that will ask the user for a number of digits to be stored. The program should then create an array of that size and then prompt the user for numbers to fill each position in the array. When all of the positions are filled, display the contents of the array to the user. Create a new Project named FillArray and a new class in the default packaged named FillArray.java. You and your partner should...

  • 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.

  • C++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise...

    C++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise a number to a power. The function should accept two arguments, the number to be raised and the exponent. Assume that the exponent is a non-negative integer. String Reverser function that accepts a string object as its argument and prints the string in reverse order. Sum of Numbers this function accepts an integer argument and returns the sum of all the integers from 1...

  • I need help with this code. Im using C++ coding. Non Recursive (iterative) Fibonacci Write a...

    I need help with this code. Im using C++ coding. Non Recursive (iterative) Fibonacci Write a program that uses a for loop to calculate a Fibonacci sequence (NOT RECURSIVE!!!) up to a given position, starting with position 0. This function defines a Fibonacci sequence: If the number is 0, the function returns a 0 If the number is 1, the function returns a 1 If the number is higher than 1, it returns the sum of the previous two numbers...

  • **C++ only and no vectors or global variables. Thank you, Write a recursive function to convert...

    **C++ only and no vectors or global variables. Thank you, Write a recursive function to convert a character string of digits to an integer. Example: convert("1234) returns 1234. Hint: To convert a character to a number, subtract the ASCII value '0' from the character, then the function can return the value s[0] - '0'.

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
ADVERTISEMENT