# Program For Prime Number In Java

Last Updated : Mar 11, 2024

In this article we will show you the solution of program for prime number in java, high numbers play a critical role in numerous mathematical algorithms and feature applications in cryptography, quantity theory, and laptop science.

knowledge a way to perceive high numbers is an vital skill for any programmer. we are able to manual you via the method of developing a Java program that assessments for top numbers, explaining each step alongside the way.

To decide if a number is prime, we will make use of the concept of divisibility.

A prime wide variety is simplest divisible with the aid of 1 and itself. via iterating from 2 to the rectangular root of the number being tested, we are able to take a look at if there are any divisors.

If a divisor is discovered, the variety isn't prime; otherwise, it's miles top.

## Step By Step Guide On Program For Prime Number In Java :-

```import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
boolean isPrime = true;
if (number <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
}
}```
1. We start with the aid of importing the `Scanner` class from the `java.util` package deal. This allows us to examine input from the user.
2. Subsequent, we create a `main` approach, that's the entry point of our application.
3. Within the `important` approach, we create an instance of the `Scanner` magnificence called `scanner` to read consumer input.
4. We prompt the consumer to go into a variety of by using using `system.out.print("enter more than a few: ")` after which name the `nextInt()` approach on the `scanner` item to examine an integer enter from the user. The entered quantity is stored inside the `number` variable.
5. We initialize a boolean variable called `isPrime` and set it to `true`. This variable will preserve song of whether or not the range is prime or not.
6. To decide if the variety is less than or identical to 1, we use an `if` statement. If the circumstance evaluates to `actual`, we recognise that the range isn't always top, so we set `isPrime` to `false`.
7. If the variety is greater than 1, we enter an `else` block, where we begin our iteration to check for divisors. We use a `for` loop to iterate from 2 to the rectangular root of the variety, inclusively.
8. In the loop, we test if the range is divisible by using the present day generation fee `i`. If the circumstance `range % i == zero` evaluates to `authentic`, it means we've observed a divisor, and therefore, the variety isn't high. We set `isPrime` to `fake` and escape of the loop the use of the `damage` key-word.
9. As soon as the loop completes, we check the fee of `isPrime`. If it's miles nonetheless `proper`, it manner the quantity is prime, and we display the message `"number is a high quantity."` using `system.out.println(range + " is a top wide variety.")`.
10. If `isPrime` is `fake`, it manner the range isn't high, and we show the message `"number isn't always a prime variety."` the usage of `system.out.println(wide variety + " isn't a high number.")`.
11. This system execution completes, and the end result is displayed to the consumer.

## Conclusion :-

I hope this article on program for prime number in java helps you and the steps and method mentioned above are easy to follow and implement.

## About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪