Friday, May 10, 2024
HomeProgramming3 different Java programs to find the next prime number

3 different Java programs to find the next prime number

Java program to find the next prime number:

In this post, we will learn how to find the next prime number in Java. The program takes one number as input and finds the next prime number, starting from that number.

The program uses one function to check whether a number is prime or not. We can use a loop to iterate over the numbers starting from the specified number, and for each number the program checks for the prime number. Once a prime number is found, that number is printed and the loop exits.

What is a prime number:

A number is called a prime number if the number is greater than 1 and it is only divisible by 1 and the number itself.

For example, 23 is a prime number. Because there is no other number than 1 And 23 who can distribute it.

In the same way, 20, 21, 22 etc. are not prime numbers.

Example 1: Java program to find the following prime number

Let’s look at the example program below:

import java.util.Scanner;

public class Main {

    private static boolean isPrime(int no) {
        for (int i = 2; i < no; i++) {
            if (no % i == 0) {
                return false;
            }
        }
        return true;
    }

    private static int getNextPrime(int no) {
        for (int i = no + 1; ; i++) {
            if (isPrime(i)) {
                return i;
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int no;

        System.out.println("Enter a number: ");
        no = scanner.nextInt();

        System.out.println("Next prime is: " + getNextPrime(no));
    }
}
  • This Java program finds the next prime number. It reads the user input number and assigns it to the No variable.
  • The receiveNextPrime function finds the next prime number.
    • It uses a for run that runs from one number after the given number and at each iteration it checks whether the current number is that fine or not.
  • To check for a prime number we use the isPrime method.
    • This method uses a for run from running away I = 2 Unpleasant i = number – 1 and possible value of i can divide the number, it returns false. Otherwise it will return WHERE.

It prints the output as below:

Example 2: By iterating up to half the number:

We can improve the prime number checking method that allows the loop to execute 2 Unpleasant number 2 just like all other numbers greater than number 2 cannot divide the number. It will reduce the loop iteration.

import java.util.Scanner;

public class Main {

    private static boolean isPrime(int no) {
        for (int i = 2; i <= no/2; i++) {
            if (no % i == 0) {
                return false;
            }
        }
        return true;
    }

    private static int getNextPrime(int no) {
        for (int i = no + 1; ; i++) {
            if (isPrime(i)) {
                return i;
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int no;

        System.out.println("Enter a number: ");
        no = scanner.nextInt();

        System.out.println("Next prime is: " + getNextPrime(no));
    }
}

The only change in the program is the for run by isPrime method. It will produce similar results.

Enter a number: 
20
Next prime is: 23

Enter a number: 
100
Next prime is: 101

Enter a number: 
101
Next prime is: 103

Example 3: By iterating to the square root of the number:

We can further optimize the above program by iterating on the square root of the number. The loop runs from 2 Unpleasant root of the number.

import java.util.Scanner;

public class Main {

    private static boolean isPrime(int no) {
        for (int i = 2; i <= Math.sqrt(no); i++) {
            if (no % i == 0) {
                return false;
            }
        }
        return true;
    }

    private static int getNextPrime(int no) {
        for (int i = no + 1; ; i++) {
            if (isPrime(i)) {
                return i;
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int no;

        System.out.println("Enter a number: ");
        no = scanner.nextInt();

        System.out.println("Next prime is: " + getNextPrime(no));
    }
}

We use Mathematics.sqrt method to find the root. It gives a similar output.

RELATED ARTICLES

2 COMMENTS

  1. you are truly a just right webmaster The site loading speed is incredible It kind of feels that youre doing any distinctive trick In addition The contents are masterwork you have done a great activity in this matter

  2. I was recommended this website by my cousin I am not sure whether this post is written by him as nobody else know such detailed about my difficulty You are wonderful Thanks

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments

бнанс Створити акаунт on The Interview Process in Clark, Pampanga
Binance注册奖金 on Venus in Scorpio