
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Check Whether a Number is Prime or Not
In this article, we will understand how to check whether a number is prime or not. Prime numbers are special numbers who have only two factors 1 and itself and cannot be divided by any other number. A number is a prime number if its only factors are 1 and itself. 11 is a prime number. Its factors are 1 and 11 itself. Some examples of prime numbers are 2, 3, 5, 7, 11, 13 and so on. 2 is the only even prime number. All other prime numbers are odd numbers.
Below is a demonstration of the same −
Input
Suppose our input is −
Enter the number : 47
Output
The desired output would be −
The number 47 is a prime number.
Algorithm
Step 1 - START Step 2 - Declare a integer value namely my_input. Step 3 - Read the required values from the user/ define the values Step 4 - Using a for loop, check if the number is divisible by any of its lower numbers except 1. If no, it is a prime number. Else it’s not a prime number. Step 5 - Display the result Step 6 - Stop
Example 1
Here, the input is being entered by the user based on a prompt. You can try this example live in our coding ground tool .
import java.util.Scanner; public class IsPrime { public static void main(String[] args) { int my_input; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.print("Enter the number : "); my_input = my_scanner.nextInt(); boolean isprime = false; for (int i = 2; i <= my_input / 2; ++i) { if (my_input % i == 0) { isprime = true; break; } } if (!isprime) System.out.println("The number " +my_input + " is a prime number."); else System.out.println("The number " +my_input + " is not a prime number."); } }
Output
Required packages have been imported A reader object has been defined Enter the number : 47 The number 47 is a prime number.
Example 2
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
public class IsPrime { public static void main(String[] args) { int my_input = 47; System.out.println("The number is defined as " +my_input); boolean isprime = false; for (int i = 2; i <= my_input / 2; ++i) { if (my_input % i == 0) { isprime = true; break; } } if (!isprime) System.out.println("The number " +my_input + " is a prime number."); else System.out.println("The number " +my_input + " is not a prime number."); } }
Output
The number is defined as 47 The number 47 is a prime number.
- Related Articles
- C++ Program to Check Whether a Number is Prime or Not
- C Program to Check Whether a Number is Prime or not?
- How To Check Whether a Number is Pointer Prime Number or Not in Java?
- How to check whether a number is a prime number or not?
- Write a Golang program to check whether a given number is prime number or not
- How to check whether a number is prime or not using Python?
- Program to check whether every rotation of a number is prime or not in Python
- C# Program to check if a number is prime or not
- Python program to check if a number is Prime or not
- PHP program to check if a number is prime or not
- Check whether N is a Dihedral Prime Number or not in Python
- C++ Program to Check Whether a Number is Palindrome or Not
- Bash program to check if the Number is a Prime or not
- Java Program to Check Whether a Character is Alphabet or Not
- Check whether the given number is Wagstaff prime or not in Python
