Back to Home page

/*******************************************************************************
Problem: Write a program to evaluate the following sum:
1/3 + 1/15 + 1/35 +....+ 1/((2n-1)*(2n+1))
where 1/((2n-1)*(2n+1)) is the n-term of the expresion.
The program shoul take a limit value for the n-term as input and output
the sum.

Course: C++
Tutor : Guillermo Julca
Mercy College

View Output
*******************************************************************************/

//---------------------------------------------------------------------------

#pragma hdrstop
#include <iostream.h>
#include <conio.h>
#include <iomanip.h> // needed for setprecision

//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{
cout<<"********************************************************************"<<endl;
cout<<"This program is going to evaluate the folowing expresion:" <<endl;
cout<<endl;
cout<<" 1/3 + 1/15 + 1/35 +....+ 1/((2n-1)*(2n+1))"<<endl;
cout<<endl;
cout<<"You have to enter a limite for the n-term e.g., 0.0001"<<endl;
cout<<"********************************************************************"<<endl;
cout<<endl;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
double lim;
cout<<"Enter the limit of the term : ";
cin>> lim;
double n ; // if we use int n then term always will be 0 for integer division
double sum;
double term;
n=1 ;
sum=0;
do
{
term = 1/((2*n)*(2*n+1)); // be carefull in the data type of n
sum = sum + term;
n++;
} while ( term > lim );
cout<<"The Sum is = "<<setprecision(15)<< sum<<endl;
getchar();

return 0;
}

View Output
//---------------------------------------------------------------------------

Back to Home Page



Copyright  © 2002                                    GJ  GUILLERMO JULCA