Java Program to Check Whether a Number is Prime or Not

Java Program to Check Whether a Number is Prime or Not

What is a Prime Number?

A prime number is a positive integer greater than 1 that is only divisible by 1 and itself. It cannot be divided evenly by any other number except 1 and itself. For example, 2, 3, 5, 7, 11, and 13 are prime numbers, while 4, 6, 8, and 9 are not.

In this Article, we will write a program to Check Whether a Number is Prime or Not.

Prime Numbers in Java :

We can check whether a given number is prime or not in java using different methods like loops, Recursion etc. We can also check prime numbers in the given intervals using these methods.

Program to Check Whether a Number is Prime or Not using For loop :

Run

// Importing required packages
import java.util.*;

public class Main{
    public static boolean isPrime(int n) {
        // for numbers less than or equal to 1
        if (n <= 1) {
            return false;
        }

        // For loop to check whether given number is prime or not
        for (int i = 2; i <= n/2; i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }
    
    public static void main(String[] args){
        // Scanner class for taking input
        Scanner scn = new Scanner(System.in);
        System.out.println("Enter the Number : ");
        int n = scn.nextInt();
        
        // Function calling for taking the ans
        boolean ans = isPrime(n);
        
        if(ans){
            System.out.println("The Given Number is Prime." );
        }else{
            System.out.println("The Given Number is Not Prime." );
        }
    }
}    
Output:

Enter the Number : 5
The Given Number is Prime.

Explanation :

The above program first checks if the number is less than or equal to 1, in which case it returns false. If the number is greater than 1, the method then iterates through all integers less than or equal to the half of the number and checks if the number is divisible by any of them. If it is, the method returns false. If the method has not returned false by the end of the loop, it returns true, indicating that the number is prime.

// For loop to check whether given number is prime or not
for (int i = 2; i <= Math.sqrt(n); i++) {
    if (n % i == 0) {
        return false;
    }
}
return true;

Program to Check Whether a Number is Prime or Not using While loop :

Run
// Importing required packages
import java.util.*;

public class Main{
    public static boolean isPrime(int n) {
        // for numbers less than or equal to 1
        if (n <= 1) {
            return false;
        }
        int i = 2;

        // While loop to check whether given number is prime or not
        while (i <= n/2) {
            if (n % i == 0){
                return false;
            }
            i++;
        }
        return true;
    }
    
    public static void main(String[] args){
        // Scanner class for taking input
        Scanner scn = new Scanner(System.in);
        System.out.print("Enter the Number : ");
        int n = scn.nextInt();
        
        // Function calling for taking the ans
        boolean ans = isPrime(n);
        
        if(ans){
            System.out.println("The Given Number is Prime." );
        }else{
            System.out.println("The Given Number is Not Prime." );
        }
    }
}    
Output:

Enter the Number : 5
The Given Number is Prime.

In the above program, the isPrime method first checks if the number is less than or equal to 1, in which case it returns false. If the number is greater than 1, the method then uses a while loop to iterate through all integers less than or equal to the half of the given number and checks if the number is divisible by any of them. If it is, the method returns false. If the method has not returned false by the end of the loop, it returns true, indicating that the number is prime.

// While loop to check whether given number is prime or not
while (i <= Math.sqrt(n)) {
    if (n % i == 0) {
        return false;
    }
    i++;
}
return true;

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription