Write an error-free Java program to do the following things. 1. Inputs two integ
ID: 3885729 • Letter: W
Question
Write an error-free Java program to do the following things. 1. 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. 2. 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. 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 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: Input integer 1: 10000000
Input integer 1: 379
Your number 379 is prime. I
nput 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, 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!!!!^^^^^
Explanation / Answer
Output
main(){
// n is count of numbers you wanna check
int n=2,i;
// using array so that we can use it for more than 2 numbers
int a[n];
// looping through count of the numbers
for(i=0;i<n;i++)
{
// asking for user input
a[i]=1;
while(a[i]<=1 || a[i]>=5000000)
{
printf(" Input integer %d:",i+1);
scanf("%d",&a[i]);
}
// checking if it is divisible by any number between 2 and half of given number
int x = 2;
while(x<a[i]/2)
{
// if it is divisible, then printing output and breaking the loop
if(a[i]%x==0)
{
printf("The smallest non-unity factor of %d is %d ",a[i],x);
break;
}
x++;
}
// if it didn't break, that means it is a prime
if(x>=a[i]/2)
{
printf("Your number %d is prime ",a[i]);
}
}
}
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.