Exercise
Function IsPrimeTarea
Objetive
Write a java function named "IsPrime", which receives an integer number and retuns true if it is prime, or false if it is not:
if (isPrime(127)) ...
Example Code
public class Main
{
public static boolean IsPrime(int n)
{
for (int i = 2; i < n; i++)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
public static void main(String[] args)
{
if (IsPrime(127))
{
System.out.println("127 is a prime number");
}
else
{
System.out.println("127 is NOT a prime number");
}
if (IsPrime(1000003))
{
System.out.println("1000003 is a prime number");
}
else
{
System.out.println("1000003 is NOT a prime number");
}
}
}