Sum of the Geometric Progression

Quote:

> The following program is supposed to compute the

> nth value in a geometric progression and then give the

> sum of the progression up to the point.

> The program gives the nth value correctly but the summation

> is always to greater than what it should be.

Did you really need to use a loop and did you reallt need to restrict

yourself to integers? If not, try something like:

#include <stdio.h>

#include <math.h>

int main(void)

{

double p, x, y;

char buf[BUFSIZ];

while (1) {

printf("enter the multipler and number of terms: ");

fflush(stdout);

if (!(fgets(buf, sizeof buf, stdin)) ||

sscanf(buf, "%lf %lf", &x, &y) != 2)

break;

if (x != 1) {

p = pow(x, y);

printf("The sum from %g through %g is %g\n", x, p,

x * (p - 1) / (x - 1));

}

else

printf("%g * 1 = %g\n", y, y);

}

return 0;

Quote:

}

/* vi: set cindent ts=4 sw=4 et tw=72: */

Quote:

> For instance when I enter 2 and 3, I get 8 correctly as

> the nth value but as a sum I get 16 when I should get

> 14.

> 2+4+8=14.

> Can anyone explain why?

> Thanx in advance

> Alan

> Here is the program:

> #include<stdio.h>

> int main( void )

> {

> int i,p,s,x,y;

> printf("enter any 2 numbers");

> scanf("%d %d",&x,&y);

> while (x!=0)

> {

> for (p=1,s=0,i=1; i<=y;i++)

> p*=x;

> s=s+p*x;

> printf("%d %d",p,s);

> printf("enter any 2 numbers");

> scanf("%d %d",&x,&y);

> }

> return 0;

> }

--

__________________________________________________________

Fight spam now!

Get your free anti-spam service: http://www.brightmail.com