Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

Illustration of if else statement

Posted By: Ella Campbell     Category: C Programming     Views: 1722

Illustration of if...else statement.

Code for Illustration of if else statement in C Programming

#define ACCURACY 0.0001                                     
                                                               
   main()                                                     
   {                                                           
       int n, count;                                           
       float x, term, sum;                                     
                                                               
       printf("Enter value of x:");                            
       scanf("%f", &x);                                        
                                                               
       n = term = sum = count = 1;                             
                                                               
       while (n <= 100)                                        
       {                                                       
          term = term * x/n;                                   
          sum = sum + term;                                    
          count = count + 1;                                   
          if (term < ACCURACY)                                 
             n = 999;                                          
          else                                                 
             n = n + 1;                                        
       }                                                       
                                                               
       printf("Terms = %d Sum = %f\n", count, sum);            
                                                               
   }                                                           
                                                               
   
    
                                                        
Output                                                     
                                                               
   Enter value of x:0                                          
   Terms = 2 Sum = 1.000000                                    
                              
   Enter value of x:0.1                                        
   Terms = 5 Sum = 1.105171                                    
                                                               
   Enter value of x:0.5                                        
   Terms = 7 Sum = 1.648720                                    
                                                               
   Enter value of x:0.75                                       
   Terms = 8 Sum = 2.116997                                    
                                                               
   Enter value of x:0.99                                       
   Terms = 9 Sum = 2.691232                                    
                                                               
   Enter value of x:1                                          
   Terms = 9 Sum = 2.718279     
  
Share: 


Didn't find what you were looking for? Find more on Illustration of if else statement Or get search suggestion and latest updates.

Ella Campbell
Ella Campbell author of Illustration of if else statement is from Toronto, Canada.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!