Summary: In this tutorial, we will learn three different methods to check whether the given number is prime or not using the Java language.

A number is said to be a prime number if it is only divisible by 1 and itself.

Examples: 5, 7, 11, 17 etc.

Prime numbers from 1 to 100

Method 1: Using For Loop

In this method, we use for loop to iterate through the numbers between 1 and n/2 and check if any of them is a factor of the n.

If so, then then n is not prime, else it is a prime number.

import java.util.Scanner;
 
public class Prime {
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
 
        System.out.print("Enter a number: ");
        int num = in.nextInt();
    
        
        boolean flag = true; //Assuming that input is Prime
        
        //Loop through all numbers between 1 and num
        for(int i=2; i<(num/2); i++){
            if(num%i == 0){
                /*
                  *If num is divisible by any number other than 1 and itself-
                  *then, it is Not Prime
                */
                flag = false; 
                break; //Since the num is not prime, so no use of iterating further
            }
        }
 
        if(flag == true){
            System.out.println("It's Prime");
        } else {
            System.out.println("Not Prime");
        }
    }
}

Output:

Enter a number: 7
It’s Prime

Method 2: Using Function

In this method, we write the logic used in the above method inside a separate function.

The function checks the number whether it is prime or not. If the number is prime it will return true otherwise false.

import java.util.Scanner;
 
public class Prime {
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
 
        System.out.print("Enter a number: ");
        int num = in.nextInt();
        
        //pass the number to the function
        if(isPrime(num)){ 
            System.out.println("It's Prime");
        } else {
            System.out.println("Not Prime");
        }
    }
 
    private static boolean isPrime(int num){
        boolean flag = true; 
        for(int i=2; i<(num/2); i++){
            if(num%i == 0){
                //If num is divisible by i, it is not Prime.
                flag = false; 
                break;
            }
        }
        return flag;
    }
}

Output:

Enter a number: 3
It’s Prime

Method 3: Using Recursion

We can also check whether a number is prime or not using a recursive function.

To the recursive function, we pass the next potential factor as i and check if it is actually the factor of the number (i.e. n%i == 0), if so then we return false otherwise, we increment i and recursively call the function until i=n/2.

If the value of i in the recursive call successfully reaches beyond n/2, then the input number is prime otherwise not.

import java.util.*;

class Main {
    public static void main(String args[]){
        Scanner in=new Scanner(System.in);
 
        System.out.print("Enter a number: ");
        int num = in.nextInt();
        
        //call the recursive function with i=2
        if(isPrime(num, 2))
            System.out.println("It's Prime");
        else
            System.out.println("Not a prime");
    }
    
    //recursive function: num=number_to_be_checked, i=potential_factor_of_num
    private static boolean isPrime(int num, int i){
        if(i==1 || i==2 || i> num/2)
            return true;
        else if(num%i == 0)
            return false;
        
        //recursive call
        return isPrime(num, i+1);
    }
}

Output:

Enter a number: 7
It’s Prime

These were the three different ways to check prime number using the Java programming language.

Leave a Reply