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:
Input
1. Integer to be processed
Output
Enter·n:·243
DigitMon·of·243·is·9
#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);
}
Comments
Leave a comment