Hello, this program should divide n/(n-1) using recursion. I need it to work like - if i do input = 6 it have to 6/5/4/3/2/1 and output = 0.05, but it does something like this 6/5*4/3*2/1 and output of this program is 3.2. I wonder what am i doing wrong.
It seems to me that you're really computing n/(n-1)!. As such, I'd just code up a factorial funciton. Then line 18 would be cout << "The result is " << static_cast<double>(n) / factorial(n-1)<<endl;
Also, when coding a recursive function, we tend to think about the recursion and forget the base case. For that reason, I always write recursive functions using the pattern:
1 2 3 4 5
if (base case) {
return the base case;
} else {
do the recursion
}
So instead of making the entire thing recursive, break it up. Make the factorial part recursive and divide the original function by the factorial.
Otherwise you could input a sentry variable, something to say that the defactorial function has been called by the user instead of by itself. That will give you a way to stop the factorial portion and divide the original input...
I haven't tested the above, but it shows the basic idea, only the user is allowed to enter true as the sentry value. (If the user inputs false it looks like they would get factorial instead which actually works on a grammatical level. Double-Negative)