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 < stdio >
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:
Comments