Wednesday, November 12, 2014

C++ program to get the sum of the series: SUM=1-(1/2!)+(1/3!)-(1/4!)...+(1/99!)-(1/100!)

#include<iostream>
using namespace std;

int main()
{
int N;
cout << "Enter the number upto which the series is to be added: ";
cin >> N;     
       double fact,even,odd,total;
even=odd=0;
for( int i=1 ; i<=N ; i++ )
{
fact=1;
for( int j=i ; j>=1 ; j-- )
fact*=j;

if( i%2==0 )
even+=(1/fact);
else
odd+=(1/fact);
}
total = odd - even;
cout << "The Sum of the series is: "
<< total
<< endl;

return 0;
}

No comments:

Post a Comment