7+ Java Prime Number Checker Program Examples

This post contains a total of 7+ Hand-Picked Java Prime number checker Program Examples with Source Code. All these prime number checker programs are made using Java Programming Language.

You can use the source code of these examples with credits to the original owner.

Related Posts

Java Prime Number Checker Programs

1. By Shams Awais

Made by Shams Awais. A java program to check if the input number is prime number or not. Source

Enter number 
11
11 is a prime number 
import java.util.*;
public class prime
{
    public static void main(String[] args) 
    {
    Scanner in=new Scanner(System.in);
    int i,n,k=0;
    System.out.println("Enter number ");
    n=in.nextInt();
    for(i=1;i<n;i++)
    {
        if(n%i==0)
        {
        k=k+1;
        }
    }
    if(k==1)
  System.out.println(n+" is a prime number ");
       else
       System.out.println(n+" is not a prime number "); 
    }
}

2. AJ

Made by AJ. Program to check if a number is prime using Recursion. Source

Enter any number : 
25 is not prime number
import java.util.*;

public class PrimeNumber
{
    public static void main(String[] args) {
        int num = 0;

        Scanner s = new Scanner(System.in);
        System.out.println("Enter any number : ");

        num = s.nextInt();
        print(num);
    }
    
    static void print(int num) {
        boolean isPrime = isPrimeNumber(num, 2);

        if(isPrime) {
            System.out.println(num + " is prime number");
        } else {
            System.out.println(num + " is not prime number");
        }
    }

    static boolean isPrimeNumber(int num, int i) {
        if(i < num) {
            if(num % i != 0) {
                return(isPrimeNumber(num, ++i));
            } else {
                return false;
            }
        }

        return true;
    }
}

3. By ASIF BILAKHIYA

Made by ASIF BILAKHIYA. This Java Program checks and prints all the prime numbers in a range. Source

First 10 Prime Number:
1	->:	2
2	->:	3
3	->:	5
4	->:	7
5	->:	11
6	->:	13
7	->:	17
8	->:	19
9	->:	23
10	->:	29
____________________
import java.util.Scanner;
class Program
{
public static void developer_asif()
    {
      System.out.printf("__________________________________________");
System.out.printf("\n|\t|\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t|\t\t\t\t\n|\t| ->Developer:Asif Bilakhiya\t\t\t\t\t\t\t\t\t|\n|\t| ->https://www.sololearn.com/Profile/928735/?ref=app\t\t\t\t\t|\n|\t|\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t|\n|\t| if(u_satisfy(true))\t\t\t\t\t\t\t\t\t\t\t\t\t\t|\n|\t| {\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t|\n|\t| like(please);\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t|\n|\t| }\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t|\n");
    System.out.printf("------------------------------------------");//
    }
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
   int  x=2;
   int c=0;
   int t=0;
   int limit=sc.nextInt();
   System.out.println("First "+limit+" Prime Number:");
   while(t!=limit)
   {
   for(int i=2;i<x;i++)
   {
     if(x%i==0)
     {
       c++;
       break;
     }
   }
   if(c==0)
    {
    System.out.print((t+1)+"\t->:\t"+x+"\n");
    t++;
    }
    c=0;
    x++;
  }
  
  developer_asif();
 }
}

4. By Sumit

Made by Sumit. A simple prime or not checker program. Source

34
not a prime
import java.util.Scanner;
public class PrimeNumber
{  
    public static void main(String[] args) {
    Scanner obj=new Scanner(System.in);
        int a=obj.nextInt();
        int d=a;
        int b=2,c=0;
          while(d!=0){
              if(d%b==0){ 
              c=c+b;
           System.out.println((c==a)?"prime":"not a prime");}
b++;}
    
      }
   }

5. By Shams Awais

Made by Shams Awais. Program to find prime number in a given range. Source

Enter a range of number : 15
2
3
5
7
11
13
import java.util.*;
class prime
{
    public static void main(String[] args) 
    {
    Scanner in=new Scanner(System.in);
    int n,p;
    System.out.println("Enter a range of number :");
    n=in.nextInt();
    for(int i=2;i<n;i++)
    {
        p=0;
        for(int j=2;j<i;j++)
        {
            if(i%j==0)
                p=1;
        }
        if(p==0)
        {
            System.out.println(i);
        
        }
    }
        
    }
}

6. By Rhea Sidana

Made by Rhea Sidana. Source

 Enter the number 
 44
 not prime number
import java.util.*;
public class pr
{
    public static void main(String[] args) {
        int x,a,b;
        b=0;
        Scanner w = new Scanner(System.in);
        System.out.println(" Enter the number ");
        x=w.nextInt();
        for(a=2; a<=x/2; ++a){
                if(x%a==0)
                {
                    b=1;
                    break;
                 }
         } 
        if (b!=1)
        { System.out.println(" prime number ");}
        else
         { System.out.println(" not prime number ");}
    }
}

7. By Qwerty

Made by Qwerty. Source

87
87 is not a prime number.
import java.util.Scanner;

class Program {

    public static void main(String[] args) {
        
        int n = new Scanner(System.in).nextInt();
        int c = 0;
        
        for(int i = 2; i*i <= n; ++i) {
            if(n % i == 0) 
                ++c;
        }
            
        if(c == 0)
            System.out.print(n + " is a prime number.");
        else
            System.out.print(n + " is not a prime number.");       
    }
}

8. By Deepak Kumar

Made by Deepak Kumar. Java Program to Check prime number. Source

11
11 is a Prime Number
public class Prime
{
    public static void main(String[] args) {
        int a =11;//number that you want to check change its value to check.another one
        int i=2;
        boolean isPrime =true;
        while(i<=(a/2))
        {
            if(a%i==0)
                isPrime=false;
            i=i+1;
        } 
        if(isPrime==true)
        System.out.println(a+" is a Prime Number");
        else if(a==1 || a==2)
            System.out.println(a+" is a Prime Number");
        else
            System.out.println(a+" is not a Prime Number");
    } 
}