home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch2 / for.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  1KB  |  35 lines

  1. /*                  for.c
  2.  *
  3.  *   Synopsis  - Three for loops. The first for loop counts by
  4.  *               1; the second for loop counts by 2; the third
  5.  *               for loop calculates a factorial.
  6.  *   Objective - To illustrate the syntax and the flexibility
  7.  *               of the for loop.
  8.  */
  9.  
  10. /* Include Files */
  11. #include <stdio.h>
  12.  
  13. /* Constant Definitions */
  14. #define LAST 7
  15.  
  16. int main( void )
  17. {
  18.      int count,
  19.          factorial;
  20.  
  21.      printf( "Counting.\n" );
  22.      for ( count = 1; count < LAST; count++ )                           /* Note 1 */
  23.           printf( "%d\n", count );
  24.  
  25.      printf( "\nCounting by two.\n" );
  26.      for ( count = 0; count < 2*LAST; )                          /* Notes 2 and 3 */
  27.           printf( "%d\n", count += 2 );                                 /* Note 4 */
  28.  
  29.      printf( "\nCalculating factorials.\n" );
  30.      for ( factorial = 1, count = 1; count <= 7; factorial *= count++ ) /* Note 5 */
  31.      ;                                                                  /* Note 6 */
  32.      printf( "%d! is %d.\n", LAST, factorial );
  33.      return 0;
  34. }
  35.