C program for Calculation of Average of Numbers

 Calculation of Average of Numbers
A program to calculate the average of a set of N numbers is given
AVERAGE OF n VALUES
Program
                
   #define       N   10               /* SYMBOLIC CONSTANT */ 
                                                               
   main()                                                     
   {                                                          
       int     count ;                  /* DECLARATION OF */  
       float   sum, average, number ;   /* VARIABLES      */  
                                                              
       sum   = 0 ;                      /* INITIALIZATION */  
       count = 0 ;                      /* OF VARIABLES   */  
                                                               
       while( count < N )                                     
       {                                                      
            scanf(“%f”, &number) ;                            
            sum   = sum + number ;                            
            count = count + 1 ;                               
       }                                                      
                                                              
       average = sum/N ;                                      
       printf(“N = %d  Sum = %f”, N, sum);                    
       printf(”  Average = %f”, average);                     
   } 
                                                        
                                                               
Output                                                     
                                                              
   1                                                          
   2.3                                                         
   4.67                                                       
   1.42                                                       
   7                                                          
   3.67                                                        
   4.08                                                       
   2.2                                                        
   4.25                                                       
   8.21                                                        
   N = 10  Sum = 38.799999  Average = 3.880000
               
      
 Average of N numbers

The variable numberis declared as float and therefore it can take both integer and real numbers.  Since the symbolic constant Nis assigned the value of 10 using the #define statement, the program accepts ten values and calculates their sum using the while loop.  The variable count counts the number of values and as soon as it becomes 11, the while loop is exited and then the average is calculated.
Notice that the actual value of sum is 38.8 but the value displayed is 38.799999.  In fact, the actual value that is displayed is quite dependent on the computer system.  Such an inaccuracy is due to the way the floating point numbers are internally represented inside the computer.

Here are the some of the book recommendations :

           

Enable GingerCannot connect to Ginger Check your internet connection
or reload the browser
Disable in this text fieldEditEdit in GingerEdit in GingerΓ—

Leave a Reply

Your email address will not be published. Required fields are marked *

Enable Notifications OK No thanks