/* * SelfTest2.question6.c * Chapter 9 * By: John Stile * Purpose: Write a program named whatnum.c that reads a positive * integer from the keyboard and then determines if it * is a prime number. * Have the program also print out the number of factors. * Use a for loop and modulus operator. * The input and output should have the following format: * * Enter a positive integer: 12 * 12 has 5 factors including 1. * * Enter a positive integer: 13 * 13 is a prime number. */ #include int main(void) { int aa, bb; // declare two ints int factors=0; // declare int and initialize to zero printf("Enter a positive integer: "); // prompt for input scanf("%d", &aa); // read from stdin, // interpret as digit, // assign to aa for( bb=aa-1; bb>0 ; bb--) // initialzie bb to one less than aa // if bb larger than zero, do block // next round, decrement bb by one { if( !(aa%bb) ) // divide aa by bb, // if there is no remainder, do block // Effectively, aa is divisable by bb, or one factorial { factors++; // increment number of factors } } if ( factors==0 ) { printf(" %d is a prime number.\n", aa); // print resutl } else { printf("%d has %d factors including 1.\n", aa, factors); // print resutl } return 0; // return 0 to calling program }