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

  1. /*                               factors.c
  2.  *
  3.  *   Synopsis  - Prompts for and accepts input of an integer
  4.  *               that is greater than 2. Displays the prime
  5.  *               factors of that integer
  6.  *
  7.  *   Objective - An example of a useful working program.
  8.  */
  9.  
  10. /* Include Files */
  11. #include <stdio.h>
  12.  
  13. int main( void )
  14. {
  15.      unsigned number, factor;          /* number will store the
  16.                                         * input, and factor
  17.                                         * drives the for loop to
  18.                                         * find the factors.
  19.                                         */
  20.  
  21.      printf( "This program will print the prime factors\n" );
  22.      printf( "of an integer that is greater than 2.\n\n" );
  23.  
  24.      /* Prompt for and input the integer. */
  25.      printf( "Enter an integer that is greater than 2 : " );
  26.      scanf( "%u", &number );
  27.  
  28.      printf( "Prime factors of %d are ", number );
  29.      for ( factor = 2; factor <= number; factor++ )
  30.  
  31.  
  32.           /* if factor divides number evenly */
  33.           if ( ! ( number % factor ) ) {
  34.  
  35.                /* output factor */
  36.                printf( "%d " , factor );
  37.                /* take the factor out of number */
  38.                number /= factor;
  39.  
  40.                /* decrement factor so that it can test
  41.                 * for a repeated factor.
  42.                 */
  43.                factor--;
  44.           }
  45.      printf( "\n" );
  46.      return 0;
  47. }
  48.