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

  1. /*               boolean.c
  2.  *
  3.  *   Synopsis  - Accepts input of a positive integer and
  4.  *               displays all its factors.
  5.  *   Objective - Illustrates the use of the enumerated type to
  6.  *               create a variable that takes values true and false.
  7.  */
  8.  
  9. /* Include Files */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12.  
  13. /* Constant Definitions */
  14. #define BUF_SIZE  80
  15.  
  16. /* Type Descriptions */
  17. enum boolean { false, true };               /* Note 1 */
  18.  
  19. int main( void )
  20. {
  21.      enum boolean prime;                    /* Note 2 */
  22.      int num, divisor;
  23.      char inarray[BUF_SIZE];
  24.  
  25.      printf( "Enter a positive integer to be tested: " );
  26.      fgets( inarray, BUF_SIZE, stdin );
  27.      num = atoi( inarray );
  28.  
  29.      if ( num <= 0 ) {
  30.           printf( "Sorry, that number wasn't positive.\n" );
  31.           exit( 1 );
  32.      }
  33.  
  34.      prime = true;                          /* Note 3 */
  35.      printf( "List of divisors: 1 " );
  36.  
  37.      for ( divisor = 2; divisor < num; divisor++ )
  38.           if ( !( num % divisor ) ) {
  39.                printf( " %d ", divisor );
  40.                prime = false;               /* Note 4 */
  41.           }
  42.  
  43.      if ( num != 1 )
  44.           printf( " %d\n", num );
  45.      else
  46.           prime = false;                    /* Note 5 */
  47.  
  48.      if ( prime )                           /* Note 6 */
  49.           printf( "%d is a prime number\n", num );
  50.      return 0;
  51. }
  52.  
  53.