Program to Check if the given Number is Prime or not in Java
Check if Number is Prime or Not
A number is said to be Prime Number if and only if it is divisible by one and itself. In this article, we will discuss about the program to check if the given number is prime or not.
- Sample Input : 17
- Sample Output : 17 is a Prime Number
Steps to Check if the number is Prime or not
- Step 1: Take input as a function parameter.
- Step 2: Check if the number is less than to 1 or not. If it is less than 1, return false.
- Step 3: Check if no is less than or equal to 3 or not. If it is less than or equal to 3, return true.
- Step 4: Now, check if no is completely divisible by 2 or 3 or not. If it is complete divisible by 2 or 3 then return false.
- Step 5: Now, initialize variable i to 5 and loop until i*i is less than or equal to or not and every time increment i with 6.
- Step 6: Check if the number if completely divisible by i or i+2 or not. If it is completely divisible then return false.
- Step 7: If the control reach to end then return true.
Code to Check if the given number is Prime or not in Java
import java.util.*;
class Solution
{
public static boolean isPrime(int no)
{
if(no<=1)
return false;
if(no<=3)
return true;
if((no%2==0) || (no%3==0))
return false;
for(int i=5;i*i<=no;i=i+6)
if((no%i==0) || (no%(i+2)==0))
return false;
return true;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a Number ");
int no=sc.nextInt();
if(isPrime(no))
System.out.println(no+" is a Prime Number");
else
System.out.println(no+" is not a Prime Number");
}
}
Output :
Enter a Number
17
17 is a Prime Number
Login/Signup to comment