I'm a beginner at c++ and i was given a certain assignment. A person enters a digit, and then this digit is multiplied by 4, but if the multiplied number is greater than 9 then we add its digits, and if the addition still give you greater than nine, then we continue to add until it gives us something equal to or less than 9. for example:
cin >> 9;
9 * 4 = 36;
since 36 > 9;
then do 3 + 6 = 9.

I don't know how to write it. Any help is appreciate it and please keep it simple since I am just a beginner.

This sounds like a good example of using recursion to achieve what you want. Basically, using a function to determine if its argument is greater than 9, and if so add the separate digits and call the function again until the input is 9 or less, than then multiply that …

## All 2 Replies

This sounds like a good example of using recursion to achieve what you want. Basically, using a function to determine if its argument is greater than 9, and if so add the separate digits and call the function again until the input is 9 or less, than then multiply that number by 4 before returning to the caller.

``````function sum_digits(digits)
// code to sum digits
end function

function recursive_function(number)
if number > 9 then
var sum_digits = sum_digits(number);
return recursive_function(sum_digits);
else
return number * 4;
end if
end function

while input > 9 then
input = recursive_function(input);
end while

output input;``````

Or at least something to that effect. :)

Set up a while loop and exit if number < 10. If not <10, add the digits.

You have to try writing something to get more detailed help...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.