Wednesday, 10 April 2013

C Program To read in two numbers x and n and then compute the sum of this geometric progression 1+x+x2+x3+……….+xn



To read in two numbers x and n and then compute the sum of this geometric progression 1+x+x2+x3+……….+xn


#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int s_sum,i,x,n;

clrscr();
printf("Enter the values for x and n:");
scanf("%d %d",&x,&n);

if(n<=0 || x<=0)
{
  printf("Value is not valid\n");
}
else
{
  printf("Value is valid\n");
  s_sum=1;
  for(i=1;i<=n;i++)
                         {
                         s_sum=s_sum+pow(x,i);                    }
                         printf("Sum of series=%d\n",s_sum);
}
getch();
                        }

No comments: