For the second program for this lab, you are to have the user enter an integer value in the range of 1 to 100. You are to verify that the user enters a value in that range, and continue to prompt him until he does give you a value in that range.
After the user has successfully entered a value in that range, you are to display all of the prime numbers up to and including the value they entered. (42 pts)
Here's an example of how the output might appear:
CSE 1020 Lab 7 Don Retzlaff donr@unt.edu
Enter a value from 1 to 100: -5
Out of range; Please try again...
Enter a value from 1 to 100: 200
Out of range; Please try again...
Enter a value from 1 to 100: 101
Out of range; Please try again...
Enter a value from 1 to 100: 45
The prime numbers are:
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43
Determining prime numbers cannot be handled with a simple mathematical formula. The most common algorithm used basically involves a process of elimination; that is, check each of the values up to one-half of the value being tested to see if the number is evenly divisible by that value. If you go through the entire list of values without finding any of them being evenly divisible, the number you're testing is a prime number.
So you basically have to individually test each of the integer values up to the value entered by your user to see if it is prime. This can be accomplished with a basic counting loop like we've done before. Inside this loop, you need to check and see if each of these values is prime. This requires the process of elimination tests mentioned previously. Basically, you have a loop that compares each number from 2 up to one-half of the value being tested to see if it evenly divides into the number you're testing.
You can use integer division to see if one number evenly divides into another: if value1 / value2 * value2 is equal to value1, then it was evenly divisible (for example: is 34 evenly divisible by 3? 34 / 3 * 3 = 33, which is not equal to 34, so it is not evenly divisible; is 20 evenly divisible by 5? 20 / 5 * 5 = 20, which indicates 5 evenly divides into 20).