My issue
For b=3 , this code gives 5 but the result should be 6. What is the error in this code
My code
#include <iostream>
using namespace std;
long int factorial(int y) {
if (y == 0 || y == 1) {
return 1;
} else {
return y * factorial(y - 1);
}
}
double result(int x) {
if (x == 1) {
return 1.0;
} else {
long int l, f;
double g;
l = factorial(x);
f = factorial(x - 2);
g = l / (f * 2);
return g + result(x - 1);
}
}
int main() {
int a, b;
cin >> a;
while (a--) {
cin >> b;
cout << result(b) << endl;
}
return 0;
}
Problem Link: Chef Fantasy 11 Practice Coding Problem - CodeChef