What is Prime number?
As per definition, Number which is greater than 1 and has only 1 divider which is itself is called Prime number
. Other numbers are called Composite Number
.
Let’s take a look: 2, 3, 5, 7, 11, 13 are prime numbers.
You are at right place if you have any of below questions.
- Prime Number Program in Java
- Java Program to Check Whether a Number is Prime or Not
- Java program to display prime numbers from 1 to 200
- How to determine a prime number in Java
- Generating Prime Numbers in Java
- Print sum of first 500 prime numbers
Sometime back I’ve written an article on how to print fibonacci series. In this tutorial we will go over below points:
- Two different ways to determine a prime number in Java
- Best way to generate prime number in Java
Let’s get started:
- Create class CrunchifyIsPrimeAndGeneratePrime.java
- create crunchifyIsPrimeNumber(int) to check if number is prime?
- create crunchifyIsPrimeNumberMethod2(int) to check number is prime or not using diff approach
- crunchifyGeneratePrimeNumbers(int) generates prime number between 2 and provided number
How to Display first N prime numbers in Java?
package crunchify.com.tutorials; import java.util.Scanner; /** * @author Crunchify.com * How to Check if Number is Prime or not in Java? How to Generate first N Prime Numbers in Java? */ public class CrunchifyIsPrimeAndGeneratePrime { public static void main(String[] args) { System.out.println(crunchifyIsPrimeNumber(11)); System.out.println(crunchifyIsPrimeNumber(22)); System.out.println(crunchifyIsPrimeNumber(37)); System.out.println(crunchifyIsPrimeNumber(7)); System.out.println("\n"); System.out.println(crunchifyIsPrimeNumberMethod2(3)); System.out.println(crunchifyIsPrimeNumberMethod2(43)); System.out.println(crunchifyIsPrimeNumberMethod2(23)); System.out.println(crunchifyIsPrimeNumberMethod2(88)); // Here 120 is an upper limit crunchifyGeneratePrimeNumbers(120); System.out.print("\n\n------ Let's find out if number is Prime or not ------ \n" + "Enter Number: "); Scanner myInput = new Scanner(System.in); System.out.println(crunchifyIsPrimeNumber(myInput.nextInt())); // Java Program to display first n prime numbers crunchifyPrintFirstNPrimeNumbers(25); } private static String crunchifyIsPrimeNumber(int crunchifyNumber) { System.out.println("Prime check started for number: " + crunchifyNumber); if (crunchifyNumber == 1) { return "1 is not a Prime number..."; } // Loop starts from 2 for (int i = 2; i <= crunchifyNumber / 2; i++) { if (crunchifyNumber % i == 0) { return crunchifyNumber + " ==> is not a Prime Number"; } } return crunchifyNumber + " ==> is a Prime Number"; } // Another way to check if Number is Prime public static String crunchifyIsPrimeNumberMethod2(int crunchifyNumber) { System.out.println("Prime check started for number: " + crunchifyNumber); if (crunchifyNumber == 1) { return "1 is not a Prime number..."; } int limit = (int) Math.sqrt(crunchifyNumber); for (int i = 2; i <= limit; i++) { if (crunchifyNumber % i == 0) { return crunchifyNumber + " ==> is not a Prime Number"; } } return crunchifyNumber + " ==> is a Prime Number"; } /* Generate Prime Numbers */ public static void crunchifyGeneratePrimeNumbers(int crunchifyUpperLimit) { System.out.println("\n------ Generating all prime number between 2 to " + crunchifyUpperLimit + " ------ "); for (int i = 2; i < crunchifyUpperLimit; i++) { boolean crunchifyIsPrime = true; // check to see if the number is prime for (int j = 2; j < i; j++) { if (i % j == 0) { crunchifyIsPrime = false; break; } } // print the number if (crunchifyIsPrime) System.out.print(i + " "); } } /* Java Program to display first n prime numbers */ public static void crunchifyPrintFirstNPrimeNumbers(int crunchifyFirstNNumber) { System.out.println("\n------ Generating first " + crunchifyFirstNNumber + " Prime Numbers ------ "); int crunchifyNumber = 3; int crunchifyCheck = 1; for (int counter = 2; counter <= crunchifyFirstNNumber; ) { for (int incrementer = 2; incrementer <= Math.sqrt(crunchifyNumber); incrementer++) { if (crunchifyNumber % incrementer == 0) { crunchifyCheck = 0; break; } } if (crunchifyCheck != 0) { crunchifyPrint(crunchifyNumber); counter++; } crunchifyCheck = 1; crunchifyNumber++; } } private static void crunchifyPrint(int crunchifyNumber) { System.out.println(crunchifyNumber + " "); } }
Run above Java Program in Eclipse IDE or IntelliJ IDEA and you should see like below.
Output:
/Library/Java/JavaVirtualMachines/jdk-15.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=60282:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 Prime check started for number: 11 11 ==> is a Prime Number Prime check started for number: 22 22 ==> is not a Prime Number Prime check started for number: 37 37 ==> is a Prime Number Prime check started for number: 7 7 ==> is a Prime Number Prime check started for number: 3 3 ==> is a Prime Number Prime check started for number: 43 43 ==> is a Prime Number Prime check started for number: 23 23 ==> is a Prime Number Prime check started for number: 88 88 ==> is not a Prime Number ------ Generating all prime number between 2 to 120 ------ 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 101 103 107 109 113 ------ Let's find out if number is Prime or not ------ Enter Number: 33 Prime check started for number: 33 33 ==> is not a Prime Number ------ Generating first 25 Prime Numbers ------ 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 Process finished with exit code 0