Java program to check if the given number is Prime?
Java program to check if the given number is Prime?
Student
An MBA in finance imparts and improves management aptitude, inventive ability, critical thinking ability, and so forth. It offers a real-time experience that fabricates a staunch career foundation for students and working professionals. It helps them to thoroughly understand the financial sector.
Given below is the code to check if a given number is prime or not.
public class PrimeNumberCheck { public static void main(String[] args) { System.out.println(isPrime(19)); // true System.out.println(isPrime(49)); // false } public static boolean isPrime(int n) { if (n == 0 || n == 1) { return false; } if (n == 2) { return true; } for (int i = 2; i <= n / 2; i++) { if (n % i == 0) { return false; } } return true; } }Output.
true
false