Java prime number program
In this article, you will learn different ways to check prime number in Java programming.
A prime number is a whole number greater than 1, whose only factors are 1 and itself, like -2, 3, 5, 7, 11 etc. For example, 17 is a prime number because it is only divisible by 1 and 17. On the other hand 18 is not a prime number because it is divisible by 2, 3, 6, 9 and the number itself.
Java check prime number using for loop
In the given Java program, we will take a number variable 'num' and check whether the number is prime or not using a for loop. Within the loop, we check whether or not the num is divisible by any number in the given range. If it is divisible, the flag is set to true and comes out of the loop. This determines the num is not a prime number. If it is not divisible, the flag is set to false and the num is a prime number.
public class CheckPrimeNumber {
public static void main(String[] args) {
int num = 23;
boolean flag = false;
for (int i = 2; i <= num / 2; ++i) {
// Check nonprime number
if (num % i == 0) {
flag = true;
break;
}
}
if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}
Output of the above code:
23 is a prime number.
Java check prime number using while loop
In the given Java program, we will take a number variable 'num' and check whether the number is prime or not using a while loop. We iterate the loop until i <= num/2. In each iteration, we check whether or not the num is completely divisible by i. If it is divisible, the flag is set to true and comes out of the loop. This determines the num is not a prime number. If it is not divisible, the num is a prime number.
public class CheckPrimeNumber {
public static void main(String[] args) {
int num = 19, i = 2;
boolean flag = false;
while (i <= num / 2) {
// Check for nonprime number
if (num % i == 0) {
flag = true;
break;
}
++i;
}
if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}
Output of the above code:
23 is a prime number.
Java check prime number using function
In the given java program, we have defined a function to check whether a number is prime or not.
public class CheckPrimeNumber{
// function to check whether a number
// is prime or not
static void checkPrimeNo(int y){
int i,x=0,flag=0;
x=y/2;
// Condition to check prime number
if(y==0||y==1){
System.out.println(y+" is not prime number");
}
else{
for(i=2;i<=x;i++){
if(y%i==0){
System.out.println(y+" is not prime number");
flag=1;
break;
}
}
if(flag==0)
{
System.out.println(y+" is prime number");
}
}
}
public static void main(String args[]){
checkPrimeNo(1);
checkPrimeNo(5);
checkPrimeNo(19);
checkPrimeNo(22);
checkPrimeNo(27);
}
}
Output of the above code:
1 is not prime number
5 is prime number
19 is prime number
22 is not prime number
27 is not prime number
Java check prime number using recursion function
In the given java program, we have defined a recursion function to check whether a number is prime or not. Recursion function is a function which is called by itself. A recursion function continues until some condition is met to prevent it. That's why we use the if else statement to break the infinite recursion.
public class CheckPrimeNumber{
static int i = 2;
// function to check whether a number
// is prime or not
public static boolean isPrimeNo(int num)
{
if(num == 0 || num == 1)
{
return false;
}
// Check Prime Number
if(num == i)
return true;
// Condition to check Prime Number
if(num % i == 0)
{
return false;
}
i++;
return isPrimeNo(num);
}
// Driver Code
public static void main(String[] args)
{
if(isPrimeNo(37))
{
System.out.println("Prime Number");
}
else
{
System.out.println("Not Prime Number");
}
}
}
Output of the above code:
Prime Number
Related Articles
Capitalize first letter of each word JavaConvert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number