

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Instructions for lab 5 in csci 1301, where students are required to write a program to identify prime numbers within a user-given range. The lab introduces the concept of nested loops and explains how to determine if a number is prime by checking divisibility. Students are encouraged to use a boolean variable to keep track of divisors and a while loop to ensure the minimum and maximum values are valid.
Typology: Lab Reports
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Csci 1301, Lab 5 2008.06. Primes.java
This lab gets us working with nested loops. You will have two lab periods to complete this one. You will be writing a program to decide which numbers within a user- given range are prime and which aren’t. Here’s a sample run:
Enter min: - min must be positive. Enter min: 3 Enter max: 2 max must be >= min. Enter max: 10
min = 3, max = 10 3 is prime. 5 is prime. 7 is prime. Checking complete.
Recall that a prime number is any integer greater than 1 that is only evenly divisible by 1 and itself. That is, a prime number has a remainder when divided by any other number. Also note that 1 is not prime.
You should use a while loop to make sure that the minimum value entered is positive. You should also use a while loop to make certain that max is greater than or equal to min. This is pretty easy. (See ExamAverager.java on page 194.)
How to figure out if an integer i is prime? Try dividing by every number greater than 1 and less i. If any of them leave a remainder of 0, then i is not prime. How are you going to test all of these numbers? A loop obviously. I would suggest keeping a boolean variable that tells you whether you’ve found a divisor or not. If you get through all of the possible divisors and don’t find one, then the number is prime and you should print a line saying this. Otherwise, it is compound.
(In fact, you only need to check those numbers that are less than the square root of i. So if a number x * x is greater than i and you haven’t found a number less than x that divides i evenly, i is prime. If that just confused you, feel free to ignore it and try every number between 1 and i .)
So you’ll need one loop to test to see if a single number i is prime. I’d recommend writing the loop to do that first. But we want to try all possible integers in the user-given range. So, just wrap that first loop inside another loop that will try every possible prime number. Easy as pie.
Csci 1301, Lab 5 2008.06. Primes.java
Remember, you’ll need an outer loop that iterates through all potential prime numbers. You’ll also need an inner loop that iterates through all potential divisors for each potential prime. If the inner loop completes without finding a divisor, you should print out that the number is prime. Finally, remember that 1 is not prime!