Java program to check prime number

A number which is divided by one and itself is called prime number. For Example 7 is a prime number because it is divided by 1 and itself only  and 8 is not a prime number because it is divided by 2 and 4 also . In other words we can say a prime number is a number which have only two factors, that is 1 and the number itself. 2 is smallest prime numbers . because 1 is smallest non -zero number have only one factor 1*1 =1 and next greater number is 2 have two factor 2*1 =2 and a  prime number  is divided by 1 and itself . so  the smallest prime number is 2.

Java Program to check whether input number is prime or not

Now we will write a program to check if a given number is prime or not in java . In this example we will take input from user using Scanner and program show output. Now we will write prime number program in java using for loop

import java.util.Scanner;

public static void main(String[] args) {
Scanner scan= new Scanner(System.in);
System.out.println("Enter any number:");
int num  =scan.nextInt();
if (flag) {
System.out.println("number " + num + " is prime number");
} else {
System.out.println("number " + num + " is  not prime number");
}
}

if (num <= 1) {
return false;
}
boolean flag = true;
for (int j = 2; j <= num / 2; j++) {
if (num % j == 0) {
flag = false;
break;
}
}
return flag;
}

}

Output :

First Input -->>
Enter any number:
10
number 10 is  not prime number
Second Input --> Enter any number: 101 number 101 is prime number

We have written prime number program in java using scanner . I have written prime number program in java using for loop now we write prime number program in java using while loopLet’s see code

import java.util.Scanner;

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter any number:");
int num = scan.nextInt();
if (flag) {
System.out.println("number " + num + " is prime number");
} else {
System.out.println("number " + num + " is  not prime number");
}
}

private static boolean isPrimeNumber(int num) {
if (num <= 1) {
return false;
}
boolean flag = true;
int j = 2;
while(j <= num / 2) {
if (num % j == 0) {
flag = false;
break;
}
j++;
}
return flag;
}
}

Output :

Enter any number:
101
number 101 is prime number

We have show prime number program in java using for loop and while loop .These examples for prime number in java iterate loop till num/2 . we can improve it and iterate loop sqrt of given number .  Now we will improve for java program to check prime number and iterate loop till sqrt of num .

import java.util.Scanner;

public static void main(String[] args) {
Scanner scan= new Scanner(System.in);
System.out.println("Enter any number:");
int num  =scan.nextInt();
if (flag) {
System.out.println("number " + num + " is prime number");
} else {
System.out.println("number " + num + " is  not prime number");
}
}

if (num <= 1) {
return false;
}
boolean flag = true;
for (int j = 2; j <= Math.sqrt(num) ; j++) {
if (num % j == 0) {
flag = false;
break;
}
}
return flag;
}

}

output :

Enter any number:
101
number 101 is prime number

Java program to check prime number using recursion

Now we will check prime number using recursion in java . we will write a method and it will return result

static int i =2;

public static void main(String[] args) {
int num =10;
if (flag) {
System.out.println("number " + num + " is prime number");
} else {
System.out.println("number " + num + " is  not prime number");
}
}

{

// Corner cases
if (num == 0 || num == 1)
{
return false;
}

// Checking Prime
if (num == i)
return true;

// Base cases
if (num % i == 0)
{
return false;
}
i++;