I need in in java by not comlicated code and not handwriting please Thank you A
ID: 3827990 • Letter: I
Question
I need in in java by not comlicated code and not handwriting please
Thank you
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. On the other hand, a composite number is a natural number greater than 1 that is not a prime number. For example, 5 is prime, as only 1 and 5 divide it whereas 6 is composite, since it has the divisors 2 and 3 in addition to 1 and 6. Write a java method isPrimeNum that takes the number n, and checks whether the number is prime or composite. The method should print at the end a message that indicates whether n is prime or composite.Explanation / Answer
public static boolean isPrimeNum(int n)
{
// number 0 and 1 are not prime
if (n <= 1)
{
System.out.println(n + " is composite");
return false;
}
// 2 and 3 are known prime
if (n == 2 || n == 3)
{
System.out.println(n + " is prime");
return true;
}
// number is divisible by 2 or 3
if (n%2 == 0 || n%3 == 0)
{
System.out.println(n + " is composite");
return false;
}
for (int i=5; i*i<=n; i=i+6)
if (n%i == 0 || n%(i+2) == 0)
{
System.out.println(n + " is composite");
return false;
}
System.out.println(n + " is prime");
return true;
}