0

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;
}
4
Contributors
8
Replies
9
Views
4 Years
Discussion Span
Last Post by on93
Featured Replies
  • 1

    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; … Read More

  • 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 … Read More

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?

0

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

0

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

1

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;
}

Edited by Gonbe: C++ style casts.. Posted too much in the C forum lately..

1

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.

Edited by Schol-R-LEA

0

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

0

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

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.