Help me in solving FIZZBUZZ2303 problem

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

@anon50671123
plzz refer my c++ code for better understanding

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    int n;
	    cin>>n;
	    n=(n)*(n-1);
	    cout<<n<<endl;
	}
	
	return 0;
}
1 Like