20 February, 2013

C Program To Find Average Of Odd & Even Numbers Upto A User-Specified Range.

This Post Contains A C Program To Find Average Of Odd & Even Numbers Upto A User-Specified Range With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'While-loop' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple
& Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C Program To Find Average Of Odd & Even Numbers Upto A User-Specified Range.

#  include  < stdio.h >
#  include  < conio.h >

void main()
{
     int  range,  i  =  0,  count  =  0,  sum  =  0;
     clrscr();
     printf ( "  \n  Enter The Range  :  " );
     scanf ( " %d ", &range );

     /*  Printing & Finding Avg Of Even No.  */
     i  =  1,  count  =  0,  sum  =  0;
     while  (  i  <=  range  )
     {
         if  (  i  %  2  ==  0  )
        {
             printf ( "  \n Even Nos. Upto %d Are  :  %d  ",  range,  i );
        }
        i++ ;
     }
     while  (  i  <=  range  )
     {
         if  (  i  %  2  ==  0  )
        {
             sum  =  sum  +  i;
             count++ ;
         }
         i++ ;
      }
      printf ( "  \n Avg Of All Even Nos. Upto %d  :  %d  ",  range,  sum  /  count );
     
      /*  Printing & Finding Avg Of Odd No.  */
     i  =  1,  count  =  0,  sum  =  0;
     while  (  i  <=  range  )
     {
         if  (  i  %  2  !=  0  )
        {
             printf ( "  \n Odd Nos. Upto %d Are  :  %d  ",  range,  i );
        }
        i++ ;
     }
     while  (  i  <=  range  )
     {
         if  (  i  %  2  !=  0  )
        {
             sum  =  sum  +  i;
             count++ ;
         }
         i++ ;
      }
      printf ( "  \n Avg Of All Odd Nos. Upto %d  :  %d  ",  range,  sum  /  count );
     
      getch();
}
/* End Of Main Program */ 

Output :

Enter The Range  :  4

Even Numbers Upto 4 Are  :  2   4
Avg Of All Even Nos. Upto 4  :  3

Odd Numbers Upto 4 Are  :  1   3
Avg Of All Odd Nos. Upto 4  :  2





No comments:

Post a Comment

Subscribe To:

Most Commonly Asked Programs In 'C' & 'C++' Language.