3) Improve the performance of your test by only checking up to the square root of n.
this is what I have done so far
import java.util.Scanner;
public class Main5
{
public static void main (String[]args)
{
Scanner myObj = new Scanner (System.in);
System.out.println (" Enter a number");
int num = myObj.nextInt ();//reading number
while (num > 0)
{
//calling prime function
boolean isPrime = isPrime (num);
if (isPrime == true)
{
System.out.println (" Entered number " + num + " is a prime");
}
else
{
System.out.println (" Entered number " + num + " is not a prime");
}
System.out.println (" Enter a number");
num = myObj.nextInt ();
}
}
public static boolean isPrime (int num)
{
boolean flag = true;
for (int i = 2; i <= num - 1; ++i)
{
// condition for nonprime number
if (num % i == 0)
{
flag = false;
break;
}
}
return flag;
}
}
I did 1 and 2 and now I need help with 3, 4 and 5 could one of you give me a hand
Step by step
Solved in 3 steps with 2 images