Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please help!! Write an error-free Java program to do the following things. 1. In

ID: 3885597 • Letter: P

Question


please help!!

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. Input integer 2: 5917 The smallest non-unity factor of 5917 is 61 Remember to put the usual header at the top of the program and to submit via Canvas.

Explanation / Answer

Source Code:

import java.util.*;

import java.lang.*;

import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */

class CheckPrime

{

// To find minimum sum of

// product of number

public static long findSmallestFactor(int num)

{

for (int i = 2; i*i<= num; i++)

{   

if (num % i == 0)

{

return i;

}

}

return num;

}

// The following method will check for the whether the entered number is prime or not

public static void CheckPrime(int num)

{

int i,m=0,flag=0;

  

m=num/2;

for(i=2;i<=m;i++){

if(num%i==0){

System.out.println(" Your Number "+num+" is not prime");

flag=1;

break;

}

}

if(flag==0)

System.out.println(" Your Number "+num+" is prime");

}

public static void main (String[] args) throws java.lang.Exception

{

int num1,num2;

Scanner sc=new Scanner(System.in);

do

{

System.out.println("Enter Integer1 and Integer2");

num1=sc.nextInt(); // Read num1

num2=sc.nextInt(); // Read num2

System.out.println("Integer 1: "+num1);

System.out.println("Integer 2: "+num2);

}while(num1>5000000|| num2>5000000);

int i,m=0,flag=0;

  

CheckPrime(num1);

System.out.println("The Smallest non unity factor of the "+num2+" is "+findSmallestFactor(num2));

}

}

Output: