JAVA PROGRAM TO FIND PRIME NUMBERS

PRIME NUMBER : Prime numbers are the numbers that are divisible by 1 and itself and must be greater than 1. For Example, 11 is a prime number since it is only divisible by 1 and itself (11). 0 and 1 are not considered as prime numbers moreover 2 is only even prime number since it is also divisible by 1 and itself (2) and all other even numbers are divisible by 2, so they are not considered as prime.

Table of Contents

[hide]
  • Java Program to check if a number is prime or not
  • Java Program to find prime numbers between two numbers
  • Java Program to find prime numbers between two numbers(Another way)
  • Java Program to find prime numbers between 1 to 100
  • Java Program to check if a number is prime or not using while loop

Example 1 : Java Program to to check if a number is prime or not

copyimage
// Java Program to to check if a number is prime or not
import java.util.Scanner;
class PrimeDemo {
   public static void findprime(int num) {
        boolean flag = true;
        if (num <= 1) { 
            flag = false;
        } 
        else {
            for (int i = 2; i <= num / 2; i++) { // non-prime condition
            if (num % i == 0) {
            flag = false;
            break;
        }
    }
}
    if (flag) {
        System.out.println(num + " " + "is a prime number");
    } else {
        System.out.println(num + " " + "is not a prime number");
    }
  }
}

public class Codebator {
   public static void main(String[] args) {
       System.out.println("Enter any Number");
       Scanner sc = new Scanner(System.in);
       int num = sc.nextInt();
       PrimeDemo.findprime(num); //calling static method
   }
}

OUTPUT :

Enter any Number17 17 is a prime number 
  • The findprime(int num) static method is used to check if the parameter passed to it is a prime number or not. In our case the number is 17.

  • Check if a number is a natural number or not. Natural Numbers are all positive integers from 1 to infinity and are mainly used for counting purpose.

  • Perform Division operation if number is not less than 1 or equal to 1.

  • If remainder is 0 then return false and break the loops, it means number is not a prime number.

  • If remainder is non-zero then return true , it means number is a prime number.

Example 2 : Java Program to to find prime numbers between two numbers

Lets make the use of our previous prime number method findprime(num) to find out prime numbers between two given numbers;

copyimage
// Java Program to to find prime numbers between two numbers
import java.util.Scanner;
class PrimeDemo {

public static void checkprime(int first,int last) {
       for(int i=first;i<=last;i++) {
          findprime(i);
       }
}
	
public static void findprime(int num) {
    boolean flag = true;
      if (num <= 1) { 
          flag = false;
      } 
      else {
          for (int i = 2; i <= num / 2; i++) { // non-prime condition
          if (num % i == 0) {
          flag = false;
          break;
       }
   }
}
    if (flag) {
       System.out.print(num+" ");
    }
  }
}

public class Codebator {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter first Number");
    int first = sc.nextInt();
    System.out.println("Enter second Number");
    int last=sc.nextInt();
    PrimeDemo.checkprime(first,last); //calling static method
}
}

OUTPUT :

Enter first Number78 Enter second Number 93 79 83 89 
  • The checkprime(first,last) static method accepts two parameters, which are used to find prime numbers between two numbers.

  • Use the same method findprime(int num) as in the above program to find the prime numbers between two numbers.

Example 3 : Java Program to to find prime numbers between two numbers(Another way)

copyimage
// Java Program to to find prime numbers between two numbers
import java.util.Scanner;
class PrimeDemo {

public static void checkprime(int first,int last) {
       for(int i=first;i<=last;i++) {
         if(findprime(i)) {
    	   System.out.println(i);
       }
    }
}
	
public static boolean findprime(int num) {
      if (num <= 1) { 
          return false;
      } 
      else {
          for (int i = 2; i <= num / 2; i++) { // non-prime condition
          if (num % i == 0) {
          return false;
           }
        }
    }
    return true;
  }
}

public class Codebator {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter first Number");
    int first = sc.nextInt();
    System.out.println("Enter second Number");
    int last=sc.nextInt();
    PrimeDemo.checkprime(first,last); //calling static method
}
}

OUTPUT :

Enter first Number78 Enter second Number 93 79 83 89 

Example 4 : Java Program to to find prime numbers between 1 to 100

copyimage
// Java Program to to find prime numbers between 1 to 100.
import java.util.Scanner;
class PrimeDemo {

public static void checkprime(int num) {
       for(int i=0;i<=num;i++) {
         if(findprime(i)) {
    	   System.out.println(i);
       }
    }
}
	
public static boolean findprime(int num) {
      if (num <= 1) { 
          return false;
      } 
      else {
          for (int i = 2; i <= num / 2; i++) { // non-prime condition
          if (num % i == 0) {
          return false;
           }
        }
    }
    return true;
  }
}

public class Codebator {
public static void main(String[] args) {
    int num=100;
    PrimeDemo.checkprime(num); //calling static method
}
}

OUTPUT :

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 

Example 5 : Java Program to to check if a number is prime or not using while loop

copyimage
// Java Program to to check if a number is prime or not using while loop
import java.util.Scanner;
class PrimeDemo {
   public static void findprime(int num) {
     int i=2;
     boolean flag = true;

     if(num<=1) {
     flag=false;
     }

    while(i<=num/2) {
      if(num%i==0) {
      flag=false;
      break;
      }
    i++;
    }
    if (flag) {
        System.out.println(num + " " + "is a prime number");
    } else {
        System.out.println(num + " " + "is not a prime number");
    }
  }
}

public class Codebator {
   public static void main(String[] args) {
       System.out.println("Enter any Number");
       Scanner sc = new Scanner(System.in);
       int num = sc.nextInt();
       PrimeDemo.findprime(num); //calling static method
   }
}

OUTPUT :

Enter any Number23 23 is a prime number