Skip to main content

Prime Number Program in Java

 Prime number in Java: Prime number is a number that is greater than 1 and divided by 1 or itself only. In other words, prime numbers can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the prime numbers.

Note: 0 and 1 are not prime numbers. The 2 is the only even prime number because all the other even numbers can be divided by 2.

Let's see the prime number program in java. In this java program, we will take a number variable and check whether the number is prime or not.

  1. public class PrimeExample{    
  2.  public static void main(String args[]){    
  3.   int i,m=0,flag=0;      
  4.   int n=3;//it is the number to be checked    
  5.   m=n/2;      
  6.   if(n==0||n==1){  
  7.    System.out.println(n+" is not prime number");      
  8.   }else{  
  9.    for(i=2;i<=m;i++){      
  10.     if(n%i==0){      
  11.      System.out.println(n+" is not prime number");      
  12.      flag=1;      
  13.      break;      
  14.     }      
  15.    }      
  16.    if(flag==0)  { System.out.println(n+" is prime number"); }  
  17.   }//end of else  
  18. }    
  19. }   
Test it Now

Output:

3 is prime number

Prime Number Program using Method in Java

  1. public class PrimeExample2{    
  2. static void checkPrime(int n){  
  3.   int i,m=0,flag=0;      
  4.   m=n/2;      
  5.   if(n==0||n==1){  
  6.    System.out.println(n+" is not prime number");      
  7.   }else{  
  8.    for(i=2;i<=m;i++){      
  9.     if(n%i==0){      
  10.      System.out.println(n+" is not prime number");      
  11.      flag=1;      
  12.      break;      
  13.     }      
  14.    }      
  15.    if(flag==0)  { System.out.println(n+" is prime number"); }  
  16.   }//end of else  
  17. }  
  18.  public static void main(String args[]){    
  19.   checkPrime(1);  
  20.   checkPrime(3);  
  21.   checkPrime(17);  
  22.   checkPrime(20);  
  23. }    
  24. }   
Test it Now

Output:

1 is not prime number
3 is prime number
17 is prime number
20 is not prime number

Prime Number Program in Java (Another way)

You can also use a method where number is not predefined. Here, user has to put the number to check if the number is prime.

  1. import java.util.Scanner;  
  2.   
  3. import java.util.Scanner;  
  4.   
  5. public class PrimeExample3 {  
  6.   
  7.    public static void main(String[] args) {  
  8.        Scanner s = new Scanner(System.in);  
  9.        System.out.print("Enter a number : ");  
  10.        int n = s.nextInt();  
  11.        if (isPrime(n)) {  
  12.            System.out.println(n + " is a prime number");  
  13.        } else {  
  14.            System.out.println(n + " is not a prime number");  
  15.        }  
  16.    }  
  17.   
  18.    public static boolean isPrime(int n) {  
  19.        if (n <= 1) {  
  20.            return false;  
  21.        }  
  22.        for (int i = 2; i < Math.sqrt(n); i++) {  
  23.            if (n % i == 0) {  
  24.                return false;  
  25.            }  
  26.        }  
  27.        return true;  
  28.    }  
  29. }  

Output:

Use image PrimeExample1

Find prime numbers between two numbers

You can also find prime numbers between two specified numbers.

  1. import java.util.Scanner;  
  2. public class PrimeExample4 {  
  3.    public static void main(String[] args) {  
  4.        Scanner s = new Scanner(System.in);  
  5.        System.out.print("Enter the first number : ");  
  6.        int start = s.nextInt();  
  7.        System.out.print("Enter the second number : ");  
  8.        int end = s.nextInt();  
  9.        System.out.println("List of prime numbers between " + start + " and " + end);  
  10.        for (int i = start; i <= end; i++) {  
  11.            if (isPrime(i)) {  
  12.                System.out.println(i);  
  13.            }  
  14.        }  
  15.    }  
  16.    public static boolean isPrime(int n) {  
  17.        if (n <= 1) {  
  18.            return false;  
  19.        }  
  20.        for (int i = 2; i <= Math.sqrt(n); i++) {  
  21.            if (n % i == 0) {  
  22.                return false;  
  23.            }  
  24.        }  
  25.        return true;  
  26.    }  
  27. }  
  28.    

Output:

Comments

Popular posts from this blog

Privacy Policy

Privacy Policy Privacy Policy This privacy policy applies to the Rhino VPN app (hereby referred to as "Application") for mobile devices that was created by Codewithyaji (hereby referred to as "Service Provider") as a Free service. This service is intended for use "AS IS". What information does the Application obtain and how is it used? The Application does not obtain any information when you download and use it. Registration is not required to use the Application. Does the Application collect precise real time location information of the device? This Application does not collect precise information about the location of your mobile device. Do third parties see and/or have access to information obtained by the Application? Since the Application does not collect any information, no data is shared with third parties. What are my opt-out rights? You can stop all collection of information by the Application easily by uninstalling it. Yo...

Exploring Methods of String Class

  Introduction String manipulation is arguably one of the most common activities in computer programming. String class has a variety of methods for string manipulation. We will discuss basic methods with examples.  public char charAt(int index) This method requires an integer argument that indicates the position of the character that the method returns.This method returns the character located at the String's specified index. Remember, String indexes are zero-based—for example, String x = "airplane"; System.out.println( x.charAt(2) ); // output is 'r' public String concat(String s) This method returns a String with the value of the String passed in to the method appended to the end of the String used to invoke the method—for example, String x = "book"; System.out.println( x.concat(" author") ); // output is "book author" The overloaded + and += operators perform functions similar to the concat()method—for example, String x = "lib...

Java increment & decrement operator

  Increment and Decrement Operator in Java : It is one of the variation of “ Arithmetic Operator “. Increment and Decrement Operators are Unary Operators. Unary Operator Operates on One Operand. Increment Operator is Used to Increment Value Stored inside Variable on which it is operating. Decrement Operator is used to decrement value of Variable by 1 (default). Types of Increment and Decrement Operator : Pre Increment / Pre Decrement Operator Post Increment / Post Decrement Operator Syntax : ++ Increment operator : increments a value by 1 -- Decrement operator : decrements a value by 1 Pre-Increment Operator : “++” is written before Variable name. Value is Incremented First and then incremented value is used in expression. “++” cannot be used over “ Constant ” of “ final Variable “. Live Example 1 : Post Incrementing Variable [468×60] class PostIncrement { public static void main ( String args [ ] ) { int num1 = 1 ; int num2 = 1 ; num1 + + ; num2 + +...