Answer to Question #330708 in C++ for Secret Agent

Question #330708

 DigitMon

I used to play Digimon.


Now that I'm a programmer and I have this weird passion of digits, I want to combine them both to create the ultimate program: DigitMon!


This DigitMon program would take an integer input and would output the sum of all the digits of the number. For example, if the input is 243, the output would be 9 because 2 + 4 + 3 = 9. In this case, we say that the DigitMon of 243 is 9.

Instructions:

  1. In the code editor, you are provided with an initial code that asks the user for an integer input and passes this to a function call of the digitMon() function.
  2. The digitMon() function is a recursive function which has the following description:
  3. Return type - int
  4. Name - digitMon
  5. Parameters - one integer
  6. Description - this function is a recursive function that computes the DigitMon of the passed integer.
  7. The digitMon() function is already partially implemented. Your task is to fill in the blanks to make it work.

Input


1. Integer to be processed

Output


Enter·n:·243
DigitMon·of·243·is·9






1
Expert's answer
2022-04-21T14:38:28-0400
#include <iostream>

using namespace std;


int digitMon(int n)
{
	static int result = 0;
	if (n > 0)
	{
		result += n % 10;
		n /= 10;
		return digitMon(n);
	}
	return result;
}

int main()
{
	int n;
	cout << "Please, enter n: ";
	cin >> n;
	cout << "DigitMon of " << n << " is " << digitMon(n);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS