Overview
For this lab we will get some practice with variables, decisions, and loops by executing some arithmetic functions and determining whether a number is prime or not. Below is the basic algorithm for implementing this lab as well as some sample output.
- Print your name and the assignment name (Lab 03, Fun With Arithmetic, whatever you like).
- Create an int variable, set it to 3.
- Calculate 3 to the first power through the sixth power.
- Subtract 125 from three to the sixth (36).
- Is the number equal to one, is it equal to two? If either of these conditions are true, it is a prime number. Print a message saying so.
- Create a boolean variable called isPrime, set it to false.
- Create a loop. Start your initializer at 2. Have the loop quit if it reaches the value of your variable.
- Inside the loop, test if the number is divisible by the current value of the counter.
- If it is, the number is not prime. Set the isPrime variable to false, print a message saying the number is not prime, then exit the loop.
- If it is not, set isPrime to true, let the loop continue running so the next number can be tested.
- Test if isPrime is true. If so print a message saying so, otherwise do nothing.
- Try recompiling the program except for step four use the value 122 instead of 125. This should create a prime number if your program is implemented correctly.
Example output:
David Whittinghill
Lab 03
Three to the first is: 3
Three to the second is: 9
Three to the third is: 27
Three to the fourth is: 81
Three to the fifth is: 243
Three to the sixth is: 729
Subtract by: 125 and get 604
Sorry, 604 IS NOT a prime number
========
David Whittinghill
Lab 03
Three to the first is: 3
Three to the second is: 9
Three to the third is: 27
Three to the fourth is: 81
Three to the fifth is: 243
Three to the sixth is: 729
Subtract by: 122 and get 607
607 IS a prime number
Grading
You will be graded on the correctness of your calculations, the readability of your code and of your output. You may not use any API calls other than println.
|