C Make Function Adds Digits Int Using Recursion Example Function Named Addalldigits Return Q37196437

C++: Make a function that adds all digits of an intusing recursion.

For example, if your function was named AddAllDigits andreturned an Int:

cout << AddAllDigits(1597); //should print 22, because 1 +5 + 9 + 7 = 22

The challenge here is using recursion: yourfunction must call itself multiple times and cannot use any loopswhatsoever. Also, no global variables allowed.

This question really stumped me. Thank you for your time!


Answer


#include <iostream>

using namespace std;

int AddAllDigits(int n){

//if n is 0 than we are done so return 0

if (n == 0)

return 0;

// extracting last digit using %

// removing the last digit by dividing with 10

return

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.