My Turbo C++ 4.5 is giving incorrect result for the following programming code which is meant to sum the series 1 + 1/1! + 1/2! + 1/3! upto 'n' terms.It gives the correct result for 'n=1' and 'n=2'.What is the problem in the following code? Please help.
#include<iostream.h>
int main()
{ int n,i,fact=1;
float sum=1.0,div;
cout << "Enter the value of n: ";
cin >> n;
if(n==1)
cout << "The sum of the series 1 + 1/1! + 1/2! +... upto " << n << " terms is " << sum ;
if(n!=1)
{ {for(i=1;i<n;i++)
{fact=fact*i;
div=(1/fact);
sum+=div;
}
}
cout << "The sum of the series 1 + 1/1! + 1/2! +... upto " << n << " terms is " << sum ;
}
return 0;
}
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/