#include <iostream>
#include "math.h"
usingnamespace std;
long factorial(long n){
if (n == 0 || n == 1) {
return 1;
}else{
return n*factorial(n-1);
}
throw"factorial error";
}
long factorialSum(long n){
long sum = 0, N = ceil(log10(n)) , num[(int)N];;
for (int i = 0; i < N; i++){
num[i] = (n/10 - floor(n/10)) * 10;
n = floor(n/10);
sum += factorial(num[i]);
cout << num[i];
}
cout << endl;
return sum;
}
The problem is in the factorialSum() function. For any number, the functions shows the digits as "0"s hence the result is the number of digits. I can't see where the error is. Any suggestion?