Sunday, May 5, 2013

Write a C program to read in two numbers, x and n, and then compute the sum of this geometric progression: 1+x+x^2+x^3+x^4------------+x^n


/*Write a C program to read in two numbers, x and n, and then compute the sum of this geometric progression:
1+x+x^2+x^3+x^4------------+x^n
( Here ^ indicates power).*/
#include<stdio.h>
#include<conio.h>
#include<math.h>

main()
{
    int i,n,x,sum=0,p;
    clrscr();
    printf("Enter X,n values:");
    scanf("%d %d",&x,&n);
    if(n>0&&x>0)
    {
        for(i=0;i<=n;i++)
        {
           p=pow(x,i);
           sum=sum+p;
        }
        printf("Sum of the geometric series %d",sum);
    }
    else
    {
        printf("error your entered -ve value \n press any key to go back Read correct pair");
    }
    getch();
}

output :

Enter X,n values:
5    3
Sum of the geometric series 156

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More