Write a program that will tell a user if their number is prime or not. Your code will need to run in a loop (possibly many loops) so that the user can continue to check numbers. A prime is a number that is only divisible by itself and the number 1. This means your code should loop through each value between 1 and the number entered to see if it’s a divisor. If you only check for a small handful of numbers (such as 2, 3, and 5), you will lose most of the credit for this project. Include a try/catch to catch input mismatches and include a custom exception to catch negative values. If the user enters 0, the program should end.
Types of Loop
Loops are the elements of programming in which a part of code is repeated a particular number of times. Loop executes the series of statements many times till the conditional statement becomes false.
Loops
Any task which is repeated more than one time is called a loop. Basically, loops can be divided into three types as while, do-while and for loop. There are so many programming languages like C, C++, JAVA, PYTHON, and many more where looping statements can be used for repetitive execution.
While Loop
Loop is a feature in the programming language. It helps us to execute a set of instructions regularly. The block of code executes until some conditions provided within that Loop are true.
PrimeAA.java
Write a program that will tell a user if their number is prime or not. Your code will need to run in a loop (possibly many loops) so that the user can continue to check numbers.
A prime is a number that is only divisible by itself and the number 1. This means your code should loop through each value between 1 and the number entered to see if it’s a divisor. If you only check for a small handful of numbers (such as 2, 3, and 5), you will lose most of the credit for this project.
Include a try/catch to catch input mismatches and include a custom exception to catch negative values.
If the user enters 0, the program should end.
Not only will you tell the user if their number is prime or not, you must also print the divisors to the screen (if they exist) on the same line as shown below AND give a count of how many divisors there are. See examples below.
Your program should run the test case exactly as it appears below, and should work on any other case in general.
Output Example
Enter a number to see if it’s prime. Enter 0 to exit.
>>>12
Divisors: 1 2 3 4 6 12
Your number, 12, is not a prime number. It has 6 divisor(s).
Enter a number to see if it’s prime. Enter 0 to exit.
>>>-32
Number cannot be negative.
Enter a number to see if it’s prime. Enter 0 to exit.
>>>17
Divisors: 1 17
Your number, 17, is a prime number. It has 2 divisor(s), 1 and itself.
Enter a number to see if it’s prime. Enter 0 to exit.
>>>nine
Invalid input.
Enter a number to see if it’s prime. Enter 0 to exit.
>>>0
You chose to exit. Goodbye.
in java
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images