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...

Privacy Policy

Privacy Policy Privacy Policy Yajindra Gautam built the Gstatus app as an Ad Supported app. This SERVICE is provided by Yajindra Gautam at no cost and is intended for use as is. This page is used to inform visitors regarding my policies with the collection, use, and disclosure of Personal Information if anyone decided to use my Service. If you choose to use my Service, then you agree to the collection and use of information in relation to this policy. The Personal Information that I collect is used for providing and improving the Service. I will not use or share your information with anyone except as described in this Privacy Policy. The terms used ...

Privacy Policy for yajindra

Privacy Policy for yajindra At instagram-analytic, accessible from https://instagram-analytic.yajindragautam.com.np, one of our main priorities is the privacy of our visitors. This Privacy Policy document contains types of information that is collected and recorded by instagram-analytic and how we use it. If you have additional questions or require more information about our Privacy Policy, do not hesitate to contact us. This Privacy Policy applies only to our online activities and is valid for visitors to our website with regards to the information that they shared and/or collect in instagram-analytic. This policy is not applicable to any information collected offline or via channels other than this website. Consent By using our website, you hereby consent to our Privacy Policy and agree to its terms. Information we collect The personal information that you are asked to provide, and the reasons why you are asked to provide it, will be made clear to you at the point we as...