Write an error-free Java program to do the following things. Inputs two integers
ID: 3884251 • Letter: W
Question
Write an error-free Java program to do the following things.
Inputs two integers from the user. (You may input both of the integers at the same time, but your best bet is to input the integers one at a time.) The limit of 2 is to simplify testing; you should write your program so that it can be easily extended to work with more than 2 numbers.
If the input integer is not between 1 and 5000000 (non-inclusive), then keep prompting the user to re-enter the number till the integer is within the proper range.
Determine if the input number is prime. If N is prime, then print a message stating that it is prime.
If the number is not prime, say it is not prime and determine the smallest factor of the number (other than 1).
To summarize, you must read in two numbers, make sure they are within in the proper range, determine if each number is prime, and, if not prime, determine its largest factor.
There are many ways to do this program, but you must include at least one for loop in your program.
Sample output:
¼¼§MInput integer 1: 10000000
¼¼§MInput integer 1: 379
MM§MYour number 379 is prime.
¼¼§MInput integer 2: 5917
MM§MThe smallest non-unity factor of 5917 is 61
Explanation / Answer
import java.util.Scanner;
public class TestCode {
static boolean isPrime(long val) {
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.