with the formula : Result = 1+2!/((x-2))-3!/((x-3))+4!/((x-4) )- n!/((x-n))
i want the output as :

Please key in x value: 8
Please key in n value: 5

result = 1 +2!/6-3!/5+4!/4-5!/3 = -33.8667

i have done the code , still cannot get the output , can anyone help me to check where the problem???

#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()
{
    int 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 <<"\nResult = 1 +2!/"<<x-2
             <<" - 3!/"<<x-3
             <<" + 4!/"<<x-4<< " - "<<n
             <<"!/"<<x-n
             <<" = "<<1+(fact(2)/x-2)-(fact(3)/x-3)+(fact(4)/x-4)-(fact(n)/x-n);




    system("pause");
    return 0;
}

Are you getting the wrong answer? Are you getting any compiler errors? Have you tried splitting the equation up to see if you are getting the values you expected?

the compiler don wan any error , it execute the wrong answer for me .

Have you tried splitting the equation up to see if you are getting the values you expected?

ya , i have try to split out the equation bfore, still cannot get the value i want

One thing to look out for is integer division. the return type of fact, x and n are all integers. Try this:

#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()
{
    int 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 <<"\nResult = 1 +2!/"<<x-2
             <<" - 3!/"<<x-3
             <<" + 4!/"<<x-4<< " - "<<n
             <<"!/"<<x-n
             <<" = "<<1+(fact(2)/static_cast<double>(x-2))-(fact(3)/static_cast<double>(x-3))+(fact(4)/static_cast<double>(x-4))-(fact(n)/(static_cast<double>(x-n)));

    return 0;
}

What is the expected result?

I suspect that you have misunderstood the last part of the summation formula. I suspect it is supposed to go more like:

         n
       ----
       \         n 
   1 + /     (-1)  n! / (x - n)
       ----
       i = 2

or,

1+2!/((x-2))-3!/((x-3))+4!/((x-4)) - ... n!/((x-n))

which means that for the example given, it would be

1+2!/((x-2))-3!/((x-3))+4!/((x-4))-5!/((x-5))

Note that if n is less than 4, the formula goes shorter than the example given. So, for example, for n = 3, the formula would simply be

1+2!/((x-2))-3!/((x-3))

The practical upshot of this is that you need to compute the sum using a loop.

i have know where is the problem already, thanks guys .

if the formula is
1+2!/((x-2))-3!/((x-3))+4!/((x-4)) - ... n!/((x-n))

what should i change for the code ?????

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.