Write an error-free Java program to do the following things:
1. Input 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.
2. If the input integer is not between 1 and 5000000 (non-inclusive), then keep prompting the user to re-enter the number until the integer is within the proper range.
3. Determine if the input number is prime. If N is prime, then print a message stating that it is prime.
4. 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 the proper range, determine if each number is prime, and, if not prime, determine its smallest factor. There are many ways to do this program, but you must include at least one for loop in your program.
Sample output:
Input integer 1: 10000000
Input integer 1: 379
Your number 379 is prime.
Input integer 2: 5917
The smallest non-unity factor of 5917 is 61
THIS DOES NOT WORK, NEEDS TO BE DONE IN ONE PUBLIC CLASS!!!!!
import java.util.*;
class IfNotPrimeListFactor {
public static boolean isPrime(int num) {
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
public static int firstFactor(int num) {
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
int SIZE = 2;
int value;
Scanner sc = new Scanner(System.in);
for (int i = 0; i < SIZE; i++) {
do {
System.out.print("Input integer " + (i + 1) + ": ");
value = sc.nextInt();
} while (value < 1 || value > 5000000);
if (isPrime(value)) {
System.out.println("Your number " + value + " is prime.");
} else {
System.out.println("The smallest non-unity factor of " + value + " is " + firstFactor(value));
}
}
}
}
BAD!!!!^^^^^