Sunday, July 14, 2019

Java Program to Check Prime Number

In this post we'll see a Java program to check whether given number is a prime number or not.

As we know that a number is a prime number if it is a natural number greater than 1 and it can be divided either by 1 or by the number itself. As example- 2, 3, 5, 7, 11, 13, 17 ….

First thing that may come to mind while writing Java program for checking prime number is to have a variable in a for loop that starts from 2 (as 1 will always divide the number) and increment it by one until it reaches the number that is checked for being prime number or not. In every iteration of the loop divide the number by variable, if remainder is zero at any time then the checked number is not a prime number.

That loop would look something like this -

for(int i = 2; i < num; i++){
    if(num % i == 0){
        flag = false;
        break;
    }
}

But that logic can be made more efficient. To check if a number is prime or not you need to run a loop starting from 2 till number/2 to check if number has any divisor.

As example- If number is 8 then you just need to check till 4 (8/2) to see if it divides by any number or not. Same way if you have a number 15 you just need to check till 7 to see if it divides completely by any number or not. We'll use the same logic to write our program to check for prime number.

Java program to check prime number or not

import java.util.Scanner;

public class PrimeCheck {

    public static void main(String[] args) {
        // take input from the user
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter number - ");
 
        int num = sc.nextInt();
        boolean flag = isPrime(num);
        if(flag){
            System.out.println(num + " is a prime number.");
        }else{
            System.out.println(num + " is not a prime number.");
        }
    }
    
    private static boolean isPrime(int num){
        boolean flag = true;
        // loop from 2, increment it till number/2
        for(int i = 2; i < num/2; i++){
            // no remainder, means divides 
            if(num % i == 0){
                flag = false;
                break;
            }
        }
        return flag;
    }
}

Output

Enter number - 
16
16 is not a prime number.

Enter number - 
31
31 is a prime number.

Here scanner class is used to get input from the user.

That's all for this topic Java Program to Check Prime Number. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Java Program to Display Prime Numbers
  2. How to Display Pyramid Patterns in Java - Part1
  3. Armstrong Number or Not Java Program
  4. Fibonacci Series Program in Java
  5. Converting int to String in Java

You may also like-

  1. Reading File in Java Using BufferedReader
  2. Count Number of Words in a String - Java Program
  3. Abstraction in Java
  4. Inheritance in Java
  5. static in Java
  6. Difference Between ReentrantLock and Synchronized in Java
  7. What is Dependency Injection in Spring
  8. Difference Between Checked & Unchecked Exception in Java

No comments:

Post a Comment