Write a program to read in two numbers, x and n, and then compute the sum of this geometric progression: 1+x+x2+x3+………….+xn.
Program Code:
#include<stdio.h>
#include<math.h>
void main()
{
int x,n,sum=0,i;
L:printf("\n enter the value of x and n: ");
scanf("%d%d",&x,&n);
if(x<0 || n<0)
{
printf("\n enter only positive values for x and n");
goto L;
}
for(i=0;i<=n;i++)
{
sum=sum+pow(x,i);
}
printf("\n x=%d,n=%d,sum of G.P=%d",x,n,sum);
getch();
}
Post A Comment:
0 comments: