Become a leader in the IoT community!
Join our community of embedded and IoT practitioners to contribute experience, learn new skills and collaborate with other developers with complementary skillsets.
Join our community of embedded and IoT practitioners to contribute experience, learn new skills and collaborate with other developers with complementary skillsets.
** What will this program output? **
“`c++
#include
unsigned long long factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n – 1);
}
int sumOfDigits(unsigned long long n) {
int sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
int main() {
unsigned long long fact = factorial(10);
std::cout << sumOfDigits(fact) << std::endl;
return 0;
}
```
||
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 π
||
CONTRIBUTE TO THIS THREAD