Firstly, factorial never adds. 7! is 5040, not 5041. 3! is 6, not 7.
If you want the result of 1+!n, you cannot add it in that location because it's recursive (it will repeat this action as well as the others).
Work it out step-by-step using 4 as a value:
Code:
factorial(4) -> result = 1 + (4 * factorial(3))
factorial(3) -> result = 1 + (3 * factorial(2))
factorial(2) -> result = 1 + (2 * factorial(1))
factorial(1) -> result = 1 + (1 * factorial(0))
factorial(0) -> return 1;
Code:
Simplified, that's:
factorial(4) = 1 + (4 * (1 + 3 * (1 + 2 * (1 + (1 * 1)))));
factorial(4) = 1 + (4 * (1 + 3 * (1 + 2 * (1 + 1))));
factorial(4) = 1 + (4 * (1 + 3 * (1 + 2 * 2)));
factorial(4) = 1 + (4 * (1 + 3 * (1 + 4)));
factorial(4) = 1 + (4 * (1 + 3 * 5));
factorial(4) = 1 + (4 * 16);
factorial(4) = 1 + 64;
factorial(4) = 65; // Should be 1+4! == 1 + 4*3*2*1 == 1 + 24 == 25
See how the "1 +" recurs over and over? That's where the problem is. Instead of doing that, you'd want something like:
Code:
public static double FactorialPlusOne(int n) {
return 1 + factorial(n);
}
Since this is not recursive, 1 will only be added once.