Question

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.

Was it helpful?

Solution

You start it with sign=-1;

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

You should start it with sign=1;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top