Monday, 13 December 2010

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

In this program we have to read the two numbers and the calculate the sum of this geometric progression in above mention .



#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();
}

Output:


1.Enter the values for x and n:2 3
Value is valid Sum of series=15

2.Enter the values for x and n:4 9
Value is valid Sum of series=21845
  3.Enter the values for x and n:01
 Value is not valid

Conclusion: the program is error free

VIVA QUESTIONS:

1) what is the difference between structures and unions ?
Ans: Here the major difference is with in the structure all elements must be allocated memory. But in union highest memory allocation must be allocated to all these elements.
 


c programs

No comments:

Post a Comment