Before start coding to check prime number, you should have a clear concept about prime number. |
What is Prime Number?
Prime number is a number that is only divisible by 1 or itself. Alternatively you can say that prime number is a natural number which is greater than 1 and only divided by 1 or itself, just like - 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ....etc. But always remember that 0 and 1 are not prime number, and 2 is the only even prime number. Because all other even numbers can be divided by 2.
Read Also : Check if a number is palindrome or not.
I hope now you have clear idea about prime number, so let's see how to check prime number in Java in different ways with examples.
Example 1:
Prime number program in java using for loop.
In this program we will take a number and with the help of 'for loop' we will check whether this number is Prime or Not.
Program Logic: In this example we have taken a number 17, as we know that no number is divisible by more than half of itself, so the loop will iterate through the values of 2 to 8(17/2=8.5), If the number 17 is completely divisible by a number from 2 to 8, then set the variable, isPrime = 1 and exit the loop.
public class CheckPrimeNumber{
public static void main(String args[]){
int number = 17; // This is the number which we have to checked.
int a = number/2; //No number is divisible by more than half of itself.
int isPrime = 0;
for(int i=2;i<=a;i++)
{
if(number%i==0)
{
System.out.println(number+" is not prime number.");
isPrime=1;
break;
}
}
if(isPrime==0)
{
System.out.println(number+" is prime number.");
}
}
}
Example 2:
Prime number program in java using while loop.
public class PrimeNumber {
public static void main(String[] args) {
int number = 41;
int k = 2;
int m = number/2; ///No number is divisible by more than half of itself.
boolean flag = true;
while (k <= m)
{
if (number % k == 0) {
flag = false;
break;
}
k++;
}
if (flag)
System.out.println(number + " is a Prime Number.");
else
System.out.println(number + " is not a Prime Number.");
}
}
Example 3:
Prime number program in java using scanner(User Input)
import java.util.Scanner;
public class PrimeNumber
{
public static void main(String[] args)
{
boolean flage=true;
Scanner scan = new Scanner(System.in);
System.out.print("Please enter the number: ");
int number = scan.nextInt();
if(number==0 || number==1)
{
System.out.println(number+" is not Prime Number.");
flage=false;
}
else{
for(int i=2;i<=number/2;i++)
{
if(number%i==0)
{
System.out.println(number+" is not Prime Number.");
flage=false;
break;
}
}
}
if(flage==true)
{
System.out.println(number+" is Prime Number.");
}
}
}
Example 4:
Check Prime Number using Method in Java.
class CheckPrimeTwo{
void CheckPrime(int k){
int i;
int flag = 0;
int a = k/2;
if(k==0||k==1)
{
System.out.println(k+" Is Not Prime Number.");
}
else
{
for(i=2;i<=a;i++){
if(k%i==0){
System.out.println(k+" Is Not Prime Number.");
flag=1;
break;
}
}
if(flag==0) { System.out.println(k+" Is Prime Number."); }
}
}
}
public class Main {
public static void main(String args[]){
CheckPrimeTwo cp = new CheckPrimeTwo();
cp.CheckPrime(3);
cp.CheckPrime(7);
cp.CheckPrime(10);
cp.CheckPrime(29);
cp.CheckPrime(25);
cp.CheckPrime(73);
cp.CheckPrime(87);
cp.CheckPrime(98);
}
}
Example 5:
Print prime numbers from 1 to 100 in java.
import java.util.Scanner;
public class CheckPrime {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in); \\ 'scan' is an object of scanner class;
System.out.print("Enter The First Number : ");
int begin_number = scan.nextInt(); \\ We are taking input from the user by the help of 'scan' object.
System.out.print("Enter The Second Number : ");
int end_number = scan.nextInt();
System.out.println("Below is the List of Prime Numbers Between " + begin_number + " and " + end_number);
int k=0;
for (int i = begin_number; i <= end_number; i++) {
if (isPrime(i)) //
{
System.out.print(i+" ");
k++; // This line is counting the total number of prime numbers.
}
}
System.out.println(" ");
System.out.println("The Total Number of Prime Number between " + begin_number + " and " + end_number+" are: "+k);
}
public static boolean isPrime(int n) {
if (n <= 1)
{
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) \\.Math.sqrt() returns the square root of vc 'n';
{
if (n % i == 0)
{
return false;
}
}
return true;
}
Note: So, there are 25 prime numbers from 1 - 100.
This is all about prime number, here basically we have discussed many ways like using for loop, using while loop, using method to check prime number in Java. In exam if you get question like "Write a program to check whether a given number is prime or not?" then you can use any method. Or if they specify any loop or method then you have to do it accordingly.
0 Comments