Signup/Sign In

Java Program To Find Prime Number

A number is called a prime number if it is divisible only by itself and one. This means that the prime numbers have only two factors one and itself.

A number is called a composite number if it has more than two factors.

A point to be noted here is that 1 is neither a prime number nor a composite number.

Conditions for a number to be prime:

  1. It should be greater than one.

  2. It shouldn't have more than 2 factors.

These are some of the first prime numbers: {2,3,5,7,11,....}.

In this tutorial, we will learn how to find prime numbers in java. But before moving forward, if you are not familiar with the concept of loops in java, then do check the article on Loops in Java

Input: Enter the number: 34

Output: 34 is not a prime number.

Program 1: Find Prime Number in Java

In this program, we will check whether the entered number is prime or not using a for loop which will run from 2 to the square root of that number.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a variable.

  4. Ask the user to initialize the variable.

  5. Call a method that will check whether the entered number is prime or not.

  6. If the number is 0 or 1, print it is not a prime number.

  7. If the number is other than 0 and 1, then run a for loop from 2 to the square root of that number.

  8. If the number is divisible by any of the numbers in that loop, then print it as not a prime number.

  9. If the number is not divisible by any of the numbers in that loop, then print it as a prime number.

  10. Stop.

Below is the Java code to find prime number.

// Program to find prime number in Java
import java.util.Scanner;
public class Main 
{  
   public static void main(String[] args) 
   {  
       Scanner sc = new Scanner(System.in);  
       System.out.println("Enter a number : ");  
       int num = sc.nextInt();  
       if (checkPrime(num)) {  
           System.out.println(num + " is a prime number");  
       }
       else 
       {  
           System.out.println(num + " is not a prime number");  
       }  
   }  
   public static boolean checkPrime(int num) 
   {  
       if (num <= 1) 
       {  
           return false;  
       }  
       for (int i = 2; i < Math.sqrt(num); i++) 
       {  
           if (num % i == 0) 
           {  
               return false;  
           }  
       }  
       return true;  
   }  
}  


Enter a number: 245
245 is not a prime number

Program 2: Find Prime Number in Java

In this program, we will check whether the entered number is prime or not using a for loop which will run from 2 to number/2.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a variable.

  4. Ask the user to initialize the variable.

  5. If the number is 0 or 1, print it is not a prime number.

  6. If the number is other than 0 and 1, then run a for loop from 2 to number/2.

  7. If the number is divisible by any of the numbers in that loop, then print it as not a prime number.

  8. If the number is not divisible by any of the numbers in that loop, then print it as a prime number.

  9. Stop.

Below is the Java code to find prime number.

// Program to find prime number in Java
import java.util.Scanner;
public class Main 
{  
   public static void main(String[] args) 
   {  
       //Take input from the user
       //Create instance of the Scanner class
       Scanner sc = new Scanner(System.in);  
       System.out.println("Enter a number : ");  
       int num = sc.nextInt();  
       int flag=0; 
       int i, temp=num/2;      
       if(num==0||num==1)
       {  
          System.out.println(num+" is not prime number");      
       }
       else
       {  
         for(i=2;i<=temp;i++)
         {      
           if(num%i==0)
           {      
               System.out.println(num+" is not a prime number");      
               flag=1;      
               break;      
           }      
         }      
         if(flag==0)  
         { 
             System.out.println(num+" is a prime number");  
         }  
        }//end of else  
   }   
}  


Enter a number: 29
29 is a prime number

Program 3: Java Program to find Prime Number

In this program, we will use recursion to check if a number is prime or not.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a variable.

  4. Ask the user to initialize the variable.

  5. Use a recursive function to check if the number is prime or not.

  6. Recursively call that function to check if that number is divisible by any other number.

  7. If the number is not divisible by any other number excluding 1 and itself, then print it as a prime number.

  8. Else print the entered number is not a prime number.

  9. Stop.

Below is the Java code to find a prime number.

// Program to find prime number in Java using recursion
import java.util.Scanner;
public class Main 
{  
    static int i=2;
   public static void main(String[] args) 
   {  
       //Take input from the user
       //Create instance of the Scanner class
       Scanner sc = new Scanner(System.in);  
       System.out.println("Enter the number: ");  
       int num = sc.nextInt();  
      
       if (checkPrime(num)) 
        {  
               System.out.print(num+" is a prime number ");  
        }
        else
        {
            System.out.println(num+" is not a prime number "); 
        }
         
   }  
    static boolean checkPrime(int num)
    {
       // 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++;
       return checkPrime(num);
    }    
} 


Enter the number: 57
57 is not a prime number



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.