Anyone who know what is the code that i have to write in to get the answer ?????

with the formula : 1+2!/((x-2))-3!/((x-3))+4!/((x-4)) - ... n!/((x-n))
#include<iostream>

using namespace std;

int fact (int no)
{
    int total;
    for (int i=0; i<=no; i++)
    {
        if(i ==0)
            total = 1;
        else
            total = total * i;
    }   

    return total;
}


int main()
{
    double x,n;
    double result;

    cout<<"Please key i x value : ";
    cin>>x;
    cout<<"\n";
    cout<<"Please key in n value : ";
    cin>>n;

    if (x<n)
    {
        cout<<"Wrong input, x valur must be greater than n !!\n"<<endl;
    }else{
        cout<<"Result = 1 + ";
        for (int loop = 2; loop< x+1; loop++)
        {
            if (loop%2 ==0)
            {
                cout<<loop<<"!/"
                    <<x-loop;
                    if (loop == x)
                    {
                        cout<<" = ";
                    }else{
                        cout<<" - ";
                    }

            }else{ 
                cout<<loop<<"!/"
                    <<x-loop;
                if (loop == x)
                    cout<<" = ";
                else
                    cout<<" + ";
            }
        }
    }

    system("pause");
    return 0;
}

As a matter of fact, I do. However, it would not be very helpful for me to simply give the code to you; learning is the goal here, after all.

I will say that you are overthinking things a bit (been there, done that, bought the tee shirt). Rather than having a conditional in which only a single character is changed between the two branches (neither of which gives you what you want), you might consider following the Sigma notation form of the series, and use a sign variable:

#include <cmath>      // add this at the top
// ...
int sign = pow(-1, i);

if you use this, and multiply the value (which will be either 1 or -1, depending on the whether the exponent is odd or even) by the result of the factorial over the difference, you can then simply accumulate the result.

result += sign * (factorial / denominator);

Another hint is to print the sign along with the value that it refers to, rather than the value before it as you have now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.