- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Check Whether a Number is Prime or Not
Prime numbers are special numbers with only two factors 1 and that number itself, they cannot be divided by any other number. Therefore, to check whether a given number is prime or not, first, we will divide the given number by all natural numbers less than or equal to it and then, we check the number of factors. If the count of factors is only two then, the given number is prime otherwise not. This way of checking prime numbers is called as factorization. Now, let's understand how we can identify if a given number is prime or not by implementing the mentioned logic.
Java Program to Check whether a number is Prime or not
Before jumping to the example programs of checking prime numbers, let's discuss the problem statement properly with the help of an example.
Instance
Input 1
1
Output
1 is not a prime number
Input 2
5
Output
5 is a prime number
Example 1
The following example illustrates how to take a number as input from the user and check whether that number is prime or not.
Approach
First, define a count variable that will store the number of factors of the given number.
Then, create an instance of the Scanner class so that we can take input from the user.
Next, take an if-else block to check whether the input is prime or not. If the input is 2, print it is prime otherwise move to the else block where we will check the number of factors using a for loop.
Now, take another if-else block to check the number of factors. If it is 2 then, print prime otherwise not a prime number.
import java.util.*; public class Example1 { public static void main(String[] args) { // initial count of factors int count = 0; // creating an instance of Scanner class Scanner sc = new Scanner(System.in); System.out.println("Enter a number to check prime number"); // to take input from user int num = sc.nextInt(); // to check prime number if(num == 2) { System.out.println(num + " is a prime number"); } else { // checking number of factors for(int i = 1; i <= num; i++) { if(num % i == 0) { count++; } } // checking number of counts to print result if(count == 2) { System.out.println(num + " is a prime number"); } else { System.out.println(num + " is not a prime number"); } } } }
Output 1
Enter a number to check prime number 13 13 is a prime number
Output 2
Enter a number to check prime number 56 56 is not a prime number
Example 2
In this Java program, instead of taking input from the user, we will initialize an integer variable to check if it is prime or not. We won't change the logic of program.
public class Example2 { public static void main(String[] args) { int num = 89; System.out.println("The given number is: " + num); // initial count of factors int count = 0; // to check prime number if(num == 2) { System.out.println(num + " is a prime number"); } else { // checking number of factors for(int i = 1; i <= num; i++) { if(num % i == 0) { count++; } } // checking number of counts to print result if(count == 2) { System.out.println(num + " is a prime number"); } else { System.out.println(num + " is not a prime number"); } } } }
Output
The given number is: 89 89 is a prime number
Conclusion
This article first explains what is the condition for a number to be called as prime and how could we check if a given number is prime or not. Then, we have written two different Java programs which use the if-else block and for loop to identify prime numbers.