CS 222
Program1
due July 7, 2016

The program

This program will prompt the user for an integer and test whether it is prime. If the integer is prime the program will display "prime". Otherwise it will display the lowest integer (greater than 1) which divides the integer. An example running the program several times:

dnordstr@dnordstr-OptiPlex-7010 ~/classes/summer16/cs222/examples $ prog1
enter a positive integer: 73771
prime
dnordstr@dnordstr-OptiPlex-7010 ~/classes/summer16/cs222/examples $ prog1
enter a positive integer: 633413
divisible by 11
dnordstr@dnordstr-OptiPlex-7010 ~/classes/summer16/cs222/examples $ prog1
enter a positive integer: 77777777
divisible by 7
dnordstr@dnordstr-OptiPlex-7010 ~/classes/summer16/cs222/examples $ prog1
enter a positive integer: 38737
prime

Internals

The program will display the prompt with printf() and accept the user's input with scanf(). You need not check that the user's input is valid. The program will then enter a while loop starting with an int factor with value 2 running until factor is half of the user's input. At each pass through the loop the program will check to see if factor divides the user's input (use the mod operator %). If it divides evenly display a message to that fact and exit the program with return. Then increase factor by one (still inside the while loop). On exit from the loop if the return wasn't taken you know that there were no divisors so you can report that the user's input is prime.

To turn in

You will turn in a hard copy of your (clearly written and well commented) program along with a hard copy of a sample terminal session showing you running your program at least four times. This input should show cases where the input was prime and was not prime. How to generate a hard copy of a terminal session will be discussed in class.