Coding Challenges <C++>
What will this program output?
Solution:Jump to solution
||
I'm sorry, my post will be long. I just want to follow the code process
Inside factorial(10)
fac(10) -> ret 10 * fac(9) -> 10 * 362,880 = 3,628,800...
2 Replies
Spoiler: 27 ...is that correct?
Solution
I'm sorry, my post will be long. I just want to follow the code process
Inside factorial(10)
fac(10) -> ret 10 * fac(9) -> 10 * 362,880 = 3,628,800
fac(9) -> ret 9 * fac(8) -> 9 * 40,320 = 362,880
fac(8) -> ret 8 * fac(7) -> 8 * 5,040 = 40,320
fac(7) -> ret 7 * fac(6) -> 7 * 720 = 5,040
fac(6) -> ret 6 * fac(5) -> 6 * 120 = 720
fac(5) -> ret 5 * fac(4) -> 5 * 24 = 120
fac(4) -> ret 4 * fac(3) -> 4 * 6 = 24
fac(3) -> ret 3 * fac(2) -> 3 * 2 = 6
fac(2) -> ret 2 * fac(1) -> 2 * 1 = 2
fac(1) -> ret 1
Inside sumOfDigits(3,628,800)
sum = 0 + 0 + 8 + 8 + 2 + 6 + 3
sum = 27
That would be 27. Again I'm sorry for the long post 😅