Home » Java Tutorial » Prime Number Program in Java

# Prime Number Program in Java

A prime number is nothing but the natural number which is divisible by 1 and itself only. 0 and 1 are not prime numbers. In the number system, the first even prime number is 2 whereas all other even prime numbers are divisible by 2. Few examples of prime number program in java are given below.

The logic of prime number

1. Take input from the user say N.
2. Divide it from 2 to N, and check the remainder
3. If the remainder is 0 then the given number is prime

## Examples of Prime number program in java

There are many ways to find the prime number in java. 4 examples are given below. Let’s understand it deeply.

## Identify given number is prime in java or not

A simple java program for the prime number to check if a given input is prime or not. Accept a number as input from the user. Apply prime number logic on it and check whether the input is prime or not.

Example

``````import java.util.Scanner;

public class Fibonacci_series {

public static void main(String[] args)
{
Scanner obj = new Scanner(System.in); // to accept input from user
boolean flag =false;
System.out.print("Enter number : ");
int n = obj.nextInt();

for (int i = 2; i <= n / 2; i++) // n/2 is the exit condition
{
if (n % i == 0) //check if number divide by any number other than given number
{
flag = true;
break; // if the number is divided by itself then break the loop
}
}
if(!flag)
{
System.out.println(n+" is a prime number");
}
else
{
System.out.println(n+" is not a prime number");
}
}
}``````

Output

``````Enter number : 4
4 is not a prime number
Enter number : 29
29 is a prime number``````

## Print prime numbers between the range in java

The below example is of identifying prime number program using scanner class. Take an input range from the user using the scanner. Iterate for loop in that range. And check if it is prime or not.

Example

``````import java.util.Scanner;
public class prime_number {

public static void main(String[] args) {
boolean flag = false;
Scanner obj = new Scanner(System.in); //scanner class to take input from user
System.out.print("Enter the starting point of the range : ");
int n = obj.nextInt();
System.out.print("Enter the ending point of the range : ");
int m = obj.nextInt();

System.out.print("Prime numbers in between "+n+" and "+m+" are : ");
for(int i = n; i <= m; i++) // loop to iterate in between range
{
for(int j = 2; j < i; j++) //loop for prime number checking
{
if(i % j == 0) //check if entered number is divided by all numbers
{
flag = false;
break;
}
else
{
flag = true;
}
}
if(flag == true) //if given number is prime number then print it
{
System.out.print(i+", ");
}
}
}
}``````

Output

``````Enter the starting point of the range : 5
Enter the ending point of the range : 100
Prime numbers in between 5 and 100 are : 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97``````

## Using method to check given number is prime number or not.

In this example, We are taking a number as an input from the user. Pass that number to the function of the prime number. If it is a prime number then the function sets the flag as true. Check if the flag is true or false. If it is true then the entered number is a prime number or if it is false the entered number is false.

Example

``````import java.util.Scanner;

public class prime{

static boolean flag =false;
void prime(int n)
{
for (int i = 2; i <= n / 2; i++) // n/2 is the exit condition
{
if (n % i == 0) //check if number divide by any number other than given number
{
flag = true; // if the number is divided by itself then break the loop
break;
}
}
}

public static void main(String[] args)
{
Scanner obj = new Scanner(System.in); // to accept input from user

System.out.print("Enter number : ");
int n = obj.nextInt();
prime p = new prime();
p.prime(n);
if(!flag)
{
System.out.println(n+" is a prime number");
}
else
{
System.out.println(n+" is not a prime number");
}
}
}``````

Output

``````Enter number : 29
29 is a prime number``````

## Check whether the given number is prime or not using recursion in java

``````import java.util.Scanner;
public class prime_number
{
int prime(int y)
{
int i = 2;
if(i < y)
{
if(y % i != 0)
{
++i;
return(prime(y));
}
else
{
return 0;
}
}
return 1;
}
public static void main(String[] args)
{
int n;
int x;
Scanner s = new Scanner(System.in);
System.out.print("Enter number:");
n = s.nextInt();
prime_number obj = new prime_number();
x = obj.prime(n);
if(x == 1)
{
System.out.println(n+" is prime number");
}
else
{
System.out.println(n+" is not prime number");
}
}

}``````

Output

``````Enter number:10
10 is not prime number``````

## How to write prime number program in java

Example

``````public class prime_number {
public static void main(String[] args) {
int n = 7, flag = 0;
for (int i = 2; i <= n / 2; i++) {

if (n % i == 0) {
flag = 1;
break;
}
}
if (flag != 1)
System.out.println(n+" number is prime");
else
System.out.println(n+" number is not prime");
}
}
``````

Output

``7 number is prime``

Shares