Ad Code

Write a program to check if the given number is prime or not?

Hello friends, today we are going to discuss a common programming topic that is prime number. As always we have seen that the question that comes up in the exam is "write a program to check whether the given number is prime or not? 
                   There are many ways to check whether a number is prime or not, and today we will discuss in detail each way with examples.

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.

                        So let's program....

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."); 
	}
  }    
}   


Check prime Number

Example 2: 

Prime number program in java using while loop.

Program Logic:  In the below program, we have used while loop instead of for loop. In this program we have taken a number 41 and The loop runs until (k <= m), where k is first divisor number and m is half of taken number (41). On each iteration, whether number(41) is divisible by 'k' is checked and the value of 'k' is incremented by 1.

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.");
 }
}


Check Prime Number using while loop

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.");
    }
  }
}



Prme 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);
  }    
}



Check prime number using method

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;  
  }  


Check Prime Number Between two number

Note: So, there are 25 prime numbers from 1 - 100.

Post a Comment

0 Comments