Check if a number is Prime or not Function Code:
public class PrimeNumF1 {
public static boolean isPrime(int n) {
// corner cases
// 2
if (n == 2) {
return true;
}
// boolean isPrime = true;
for (int i = 2; i <= n - 1; i++) {
if (n % i == 0) { // Completely dividing
// isPrime = false;
// We any use any technic in comment options.
// break;
// return isPrime;
return false;
}
}
// return isPrime;
return true;
}
public static void main(String[] args) {
System.out.println(isPrime(7907));
}
}
java .\PrimeNumF1.java
true
No comments:
Post a Comment