0

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.

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by WaltP
0

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

read input;

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

output input;

Or at least something to that effect. :)

Edited by dohpaz42: n/a

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.