Thursday, 28 June 2012

c++ program that calculate the sum of : 1/2-2/3+3/4-4/5+…..n terms.

#include<iostream.h>
#include<conio.h>
int main()
{
int n;
float sum=0.0,i,j;
clrscr();
cout<<"Enter any number : ";
    cin>>n;



for(i=1,j=2;i<=n;i++,j++)
{
    if((int)i%2!=0)
        sum=sum+i/j;
    else
        sum=sum-i/j;
}
cout<<sum;
getch();
}