Wednesday, February 23, 2011

Decision Making and Looping

A program to evaluate the series
1/(1-x) = 1 + x + x to power 2 + x to power 3 + ..... + x to power n

#include <stdio.h>
#define   LOOP       100
#define   ACCURACY   0.0001
void main()
   {                                                              
       int n;                                                     
       float x, term, sum;                                        
                                                              
       printf("Input value of x : ");                              
       scanf("%f", &x);                                       
       sum = 0 ;                                              
       for (term = 1, n = 1 ; n <= LOOP ; ++n)
       {                                                          
           sum += term ;                                      
    if (term <= ACCURACY)
              goto output; /* EXIT FROM THE LOOP */               
           term *= x ;                                        
       }                                                      
       printf("\nFINAL VALUE OF N IS NOT SUFFICIENT\n");            
       printf("TO ACHIEVE DESIRED ACCURACY\n");                   
       goto end;                                                  
       output:                                                    
       printf("\nEXIT FROM LOOP\n");                                
       printf("Sum = %f;  No.of terms = %d\n", sum, n);            
       end:                                                       
       ;       /* Null Statement */                                                   
       getch();
   }

No comments:

Post a Comment