Programming Exercise 9 |
< Previous Next > |
A prime number is a number that is only evenly divisible by itself and 1. Write a function named is_prime(N) where N is an integer argument and the function returns the first divisor if the argument is not prime, or 1 otherwise. For instance, if the input is 5, the function will return 1 whereas if the input was 6 it would return 2. Use the function in a program that prompts the user to enter a number and then prints whether the number is prime based on the return value. The program should also check that the input is positive and print an appropriate error message if not.
Please enter a positive integer: 9 The integer 9 is not prime because it is divisible by 3
Please enter a positive integer: 11 The integer 11 is prime!
Please enter a positive integer: 100 The integer 100 is not prime because it is divisible by 2
Please enter a positive integer: 169 The integer 169 is not prime because it is divisible by 13
Please enter a positive integer: -3 -3 is not a positive integer. Please enter a positive integer: -169 -169 is not a positive integer. Please enter a positive integer: -9 -9 is not a positive integer. Please enter a positive integer: 9 The integer 9 is not prime because it is divisible by 3
handin pe9 pe9.pyTo verify your submission, type the following in a terminal window:
handin pe9