Java Check Prime Number: Hey there! We’re about to create a little program in Java that can tell us if a number is prime or not. “prime” numbers, are a bit like superheroes in the world of math. They can only be divided by themselves and 1, and that’s what makes them unique.
So, with our Java program, we’re going to check out if a number is prime or not. It’s going to be a fun and straightforward journey, so let’s dive right in and start figuring out which numbers are prime in the world of Java!
Concept of Prime number
A prime number is a whole number greater than 1 that has only two distinct positive divisors: 1 and itself.
For Example – Numbers like 2, 3, 5, 7, and 11 are prime numbers and other Numbers like 4, 6, and 9 are not prime because they have more than two divisors.
Java Program to check if a Number is Prime
To check if a number is prime in Java, you can use a simple algorithm that tests whether the number is divisible by any integer from 2 to the square root of the number. If it is not divisible by any of these integers, then it is a prime number. Here’s a Java program to do this:
Program – Write a Java program to check if a number is prime or not
Algorithm
Here’s the algorithm for checking whether a given number is prime or not:
- Start
- Read the number to be checked for primality, which we’ll call `num`.
- If `num` is less than or equal to 1, go to step 11. (0 and 1 are not prime numbers)
- Initialize a variable `isPrime` to `true`. This variable will be used to keep track of whether `num` is prime or not.
- Initialize a variable `divisor` to 2.
- While `divisor` is less than or equal to the square root of `num`, repeat steps 7-9.
- Check if `num` is divisible by `divisor` (i.e., `num % divisor == 0`). If it is, set `isPrime` to `false`, and go to step 10.
- Increment `divisor` by 1.
- End of the loop.
- If `isPrime` is still `true`, then `num` is a prime number. Display that `num` is prime.
- If `isPrime` is `false`, then `num` is not a prime number. Display that `num` is not prime.
- End
Source Code
You can check the prime number with these simple steps:-
import java.util.Scanner; public class PrimeChecker { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int numberToCheck = scanner.nextInt(); scanner.close(); if (isPrime(numberToCheck)) { System.out.println(numberToCheck + " is a prime number."); } else { System.out.println(numberToCheck + " is not a prime number."); } } public static boolean isPrime(int num) { if (num <= 1) { return false; } for (int i = 2; i * i <= num; i++) { if (num % i == 0) { return false; } } return true; } }
Steps to Write Program
Step-1: Import the `Scanner` class to enable user input handling.
import java.util.Scanner;
Step-2: Declare a class named `PrimeChecker` to encapsulate the functionality of checking prime numbers.
public class PrimeChecker { }
Step-3: Define the `main` method, the program’s entry point.
public static void main(String[] args) { }
Step-4: Create a `Scanner` object named `scanner` for user input.
Scanner scanner = new Scanner(System.in);
Step-5: Display a prompt asking the user to enter a number to check if it’s prime.
System.out.print("Enter a number: ");
Step-6: Read the user’s input as an integer and store it in the variable `numberToCheck`.
int numberToCheck = scanner.nextInt();
Step-7: Close the `Scanner` to release system resources after input is obtained.
scanner.close();
Step-8: Check if the entered number is prime using the `isPrime` method.
if (isPrime(numberToCheck)) {} else{}
Step-9: If the `isPrime` method returns `true`, print a message stating that the number is prime. Otherwise, print a message stating that it’s not prime.
if (isPrime(numberToCheck)) { System.out.println(numberToCheck + " is a prime number."); } else { System.out.println(numberToCheck + " is not a prime number."); } }
Step-10: Define the `isPrime` method to check whether a given number is prime.
public static boolean isPrime(int num) { }
Step-11: Check if the input number is less than or equal to 1. If so, return `false` because 0 and 1 are not prime numbers.
if (num <= 1) { return false; }
Step-12: Iterate through numbers from 2 to the square root of the input number.
for (int i = 2; i * i <= num; i++) { }
Step-13: Within the loop, check if the input number is divisible by the current iteration value. If it is, return `false` because a divisor has been found, indicating that the number is not prime.
if (num % i == 0) { return false; }
Step-14: If the loop completes without finding any divisors, return `true` to indicate that the input number is prime.
return true;
Output
Compile and run your Java program. It will ask the user for a number and then tell them whether it’s prime or not.
Suppose you enter the number 7
Enter a number: 7 7 is a prime number.
Suppose you enter a number 12
Enter a number: 12 12 is not a prime number.