Computers in Engineering WWW Site - Example 11.2

Example 11.2


C Version

/*
  Demonstration of a simple while{} loop.
*/

#include <stdio.h>


main()
{
  /*  Declaration Statements  */

  double sum, count, ave;

  /*  Assignment Statements  */

  printf("C32.C -> Program to demonstrate simple while{} loop.\n");
  printf("\nEnter Sum :");
  scanf("%lg", &sum);
  printf("\nEnter Count ( 0 to stop the program ) :");
  scanf("%lg",&count);
  getchar();
  while (count != 0.0) {
    ave = sum / count;
    printf("sum =% .5E\n", sum);
    printf("count =% .5E\n", count);
    printf("average =% .5E\n", ave);
    printf("==========================\n\n");
    printf("Enter Sum :");
    scanf("%lg", &sum);
    printf("Enter Count ( 0 to stop the program ) :");
    scanf("%lg",&count);
    getchar();
    clrscr();  /*  To clear screen before new entry  */
  }

  return(0);
}
/*  End of Program C32  */
/*
INPUT :
234
5
346
234
0
0

OUTPUT :
C32.C -> Program to demonstrate simple while{} loop.
Enter Sum :234
Enter Count ( 0 to stop the program ) :5
sum = 2.34000E+002
count = 5.00000E+000
average = 4.68000E+001
==========================

Enter Sum :346
Enter Count ( 0 to stop the program ) :234
sum = 3.46000E+002
count = 2.34000E+002
average = 1.47863E+000
==========================

Enter Sum :0
Enter Count ( 0 to stop the program ) :0

*/

Last modified: 21/07/97