Please Help

I made a program that i got as homework.
the question is: Write a C++ program to sum the sequence if
2/9 - 5/13 + 8/17....
till the number of terms the user wants.


I made the following program, but the answer seems to be wrong. Can someone please help me point out where have i gone wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i,n,a=2,b=9,count=1;
cout<<"Enter the number of terms in the series: ";
cin>>n;
float sum=a/b;
	for(i=0;i<=n;i++)
	{
	sum=sum+pow(-1,count)*(a+3)/(b+4);
	count++;
	}
	cout<<"\nThe sum of the series is = "<<sum;
getch();
}
Last edited on
a and b are ints, so a/b is rounded down to 0. Make them float or double or add a cast sum = float(a)/b;
i jsut tried that
if i enter the number of terms n to be odd
the output screen shows the sum = 0.22222
and if i enter the number of terms to be even
then the output screen shows= 0.162393
which is the sum of the first term only
Last edited on
That's because you never change the values of a and b. You just keep adding or subtracting 5/13 from sum.

I just noticed there is a bunch of mess in your code.
iostream header should be without ".h", math header in C++ is called <cmath> (also without ".h"). conio.h is not standard, nor is it needed. main must be int, not void, cin and cout are in std namespace. Your loop runs n+1 cycles so n+2 terms are computed. count variable is useless as it is (in your code) equal to i+1.
Topic archived. No new replies allowed.