Urgent need help with1+2!/(x-2)-3!/(x-3)+4!/(x-4) - … +/- n!/(x-n)

Write a simple program to prompt the user to key in x and n value to calculate the result for the following formula:

Result = 1+2!/((x-2))-3!/((x-3))+4!/((x-4) )- … n!/((x-n))


#include <iostream>
using namespace std;

int main (int argc, char *argv[])
{
int x,n;
cout<<"Please enter values for x : ";
cin>>x;
cout<<"Please enter values for n : ";
cin>>n;
float product=2.0;
float sum = n;
float fact=2.0;
float c = 1.0;

for (int i=2; i<=n ; i++)
{
sum = 2.0/(x-n);
product= product *= i;
fact=fact *=i/(x-2);

if(i%2==0) //every two times
sum+=(product/fact);
else
sum-=(product/fact);
}



cout<<"The answer is: "<<sum<<endl;
system("pause");
return 0;
}
Last edited on
Urgent need question not psychic.
Topic archived. No new replies allowed.