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 >
Wrap
C/C++ Source or Header
|
2005-06-16
|
1KB
|
53 lines
/* boolean.c
*
* Synopsis - Accepts input of a positive integer and
* displays all its factors.
* Objective - Illustrates the use of the enumerated type to
* create a variable that takes values true and false.
*/
/* Include Files */
#include <stdio.h>
#include <stdlib.h>
/* Constant Definitions */
#define BUF_SIZE 80
/* Type Descriptions */
enum boolean { false, true }; /* Note 1 */
int main( void )
{
enum boolean prime; /* Note 2 */
int num, divisor;
char inarray[BUF_SIZE];
printf( "Enter a positive integer to be tested: " );
fgets( inarray, BUF_SIZE, stdin );
num = atoi( inarray );
if ( num <= 0 ) {
printf( "Sorry, that number wasn't positive.\n" );
exit( 1 );
}
prime = true; /* Note 3 */
printf( "List of divisors: 1 " );
for ( divisor = 2; divisor < num; divisor++ )
if ( !( num % divisor ) ) {
printf( " %d ", divisor );
prime = false; /* Note 4 */
}
if ( num != 1 )
printf( " %d\n", num );
else
prime = false; /* Note 5 */
if ( prime ) /* Note 6 */
printf( "%d is a prime number\n", num );
return 0;
}