Thursday, 13 September 2012

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


Description:
In this program we have to read the two numbers and the calculate the sum of this geometric progression in above mention .
Algorithm:
Step 1: Start
Step 2: read values of x and n, sum-1, i=1
Step 3: check for n & X
i)                    if n<=0 || x<=0
ii)                  print values are not valid
iii)                read values of x and n
Step 4: perform the loop operation
i)                    for(i=1;i<=n;i++) then follows
ii)                  sum=sum+pow(x,i)
Step 5: print sum
Step 6: Stop
 Program:


#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:0
   1
   Value is not valid
Conclusion: the program is error free

VIVA QUESATIONS:

1) what are 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 the all these elements.




No comments:

Post a Comment