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.
C# Tip: DO NOT use nameof to value constants
Programming in C# involves making numerous decisions to ensure code efficiency and maintainability. One common aspect is dealing with constants,…
Can we run a Java program without the main method
Can we run a Java program without the main method? The mainly method is executed when we execute a class in…
Java program to calculate students grades in 2 different ways
Java program to calculate students grades: In this post we will learn different ways to calculate the figure from a student…
C# Tip: LINQ’s Enumerable.Range to generate a range of consecutive numbers
When you need to generate a series of numbers in ascending order, you can simply use a while loop with…
Java String charAt method explanation with examples
Java String charAt method: The signAt method of Java string is used to find a character at a given index. This…
3 Ways to Convert System.nanoTime to Seconds in Java
How to convert System.nanoTime to seconds in Java: In this post we will learn how to convert System.nanoTime Unpleasant seconds in…
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
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