A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number.
public static boolean isPrime(int num){
for(int i=2;i<=num/2;i++){
if(num % i == 0){
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println("IS 17 PRIME NUMBER : "+NumberIsPrimeOrNot.isPrime(17));
System.out.println("IS 5 PRIME NUMBER : "+NumberIsPrimeOrNot.isPrime(5));
}
}
Program Output :-
IS 17 PRIME NUMBER : true
IS 5 PRIME NUMBER : true

No comments:
Post a Comment