Computers in Engineering WWW Site - Example 12.2

Example 12.2


C Version

/*
  Demonstration of while loops and the formatting codes for printf
*/

#include <stdio.h>

main()
{
     /*  Declaration Statements  */
     short i, j;
     double ks, lt;
     printf("C42.C -> While loops and printf formatting codes.\n");

     /*  Initializing variables  */
     ks = 0.0;
     lt = 0.0;
     i = 1;

     /*  Assignment Statements  */
     while (i <= 10) {
          for (j = 1; j <= 10; j++)
               ks += j;
          lt += i;
          printf("i = %d, ks = % .5E, lt = % .5E\n", i, ks, lt);
          i += 2;
     }  /*  End of while{} loop  */

return(0);
}
/*  End of Program C42  */
/*
OUTPUT :

C42.C -> While loops and printf formatting codes.
i = 1, ks =  5.05000E+03, lt =  1.00000E+00
i = 3, ks =  1.01000E+04, lt =  4.00000E+00
i = 5, ks =  1.51500E+04, lt =  9.00000E+00
i = 7, ks =  2.02000E+04, lt =  1.60000E+01
i = 9, ks =  2.52500E+04, lt =  2.50000E+01

*/

Last modified: 21/07/97