user2747954
user2747954

Reputation: 97

sum of the series in c++

I tried a lot to find the problem in my code but not able to figure out why output is not coming correct. My question is to sum the sequence 2/9 - 5/13 + 8/17.... and here is my code I AM NOT GETTING CORRECT RESULT.

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n,sign=-1;
float a=2,b=9;
clrscr();
cout<<"Enter the number of terms in the series: ";
cin>>n;
float sum = a/b;
for(i=1;i<=n;i++)
{
cout<<a<<"/"<<b<<" "<<sign<<" "<endl;
a=a+3;
b=b+4;
sign= -1*sign;
sum+=sign*(a/b);
}
cout<<"\nThe sum of the series is = "<<sum;
getch();
}

0.660059

Kindly tell me where I am wrong.

Upvotes: 2

Views: 1574

Answers (1)

Jerry
Jerry

Reputation: 4408

You start it with sign=-1;

then sign= -1*sign; before adding the second term.

You should start it with sign=1;

Upvotes: 5

Related Questions