Skip to main content

Posts

Showing posts from August 29, 2010

A C program to print the average of the numbers(Using While Loop)

Below is a program to print the average of two numbers that are input through the keyboard.   /* Read the numbers from the keyboard as input and * print out their average as output. */    #include &lt stdio &gt int main( ) { double x = 0.0, sum = 0.0; int count = 0; printf( "\t--- Calculate Average of the numbers ---\n" ); printf( "\nEnter the number you want to calulate average for:\n" "(Type a letter to end your input)\n" ); while ( scanf( "%lf", &x ) == 1 ) { sum += x; ++count; } if ( count == 0 ) printf( "No input data given!\n" ); else printf( "The average of given numbers is %.2f\n", sum/count ); return 0; } The output of the program: