Here we generate and check prime number using while loop in java so that we titled this **prime number program in java using while loop**.

## 1 What is a Prime Number

A prime number is a whole number that has only two factors 1 and the number itself.

For example, 23 is a prime number, because it has only two factors 1 and 23.

To solve this we used following concepts

## 2 Java program to check prime number

Java program to find prime number can be divided in following steps.

- Take a number
- start a loop from 2 to number/2 times
- check whether a number is divisible in between
- if divisible then increase count variable by one and break loop
- after loop check if count variable in zero then number is prime otherwise not a prime number.

Prime no in java can be checked very easily with above steps.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.Scanner; public class PrimeNumberChecker { public static void main(String[] args) { System.out.println("Enter a number to check Prime or Not"); Scanner scanner = new Scanner(System.in); int number = scanner.nextInt(); int i = 2, count = 0; while (i <= number / 2) { if (number % i == 0) { count++; break; } i++; } if (count == 0) { System.out.println(number + " is prime number"); } else { System.out.println(number + " is not a prime number"); } } } |

Output

1 2 3 | Enter a number to check Prime or Not 23 23 is prime number |

## 3 Prime number program in java using scanner

We have created a java static method to check prime number in java

This is another way to check Prime no program in java.

Here a method is created and specified the work to check prime no in this static method.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.util.Scanner; public class PrimeNumberChecker1 { public static boolean isPrime(int number) { int i = 2, count = 0; boolean status; while (i <= number / 2) { if (number % i == 0) { count++; break; } i++; } status = count == 0 ? true : false; return status; } public static void main(String[] args) { System.out.println("Enter a number to check Prime or Not"); Scanner scanner = new Scanner(System.in); int number = scanner.nextInt(); if(isPrime(number)) { System.out.println(number + " is prime number"); } else { System.out.println(number + " is not a prime number"); } } } |

Output

1 2 3 4 5 6 7 | Enter a number to check Prime or Not 23 23 is prime number Enter a number to check Prime or Not 55 55 is not a prime number |

## 4 Java program for prime number in range

To find prime numbers in java program we can use following source code.

Prime numbers program in java Takes two integer numbers for range and generate prime numbers between range.

java program for prime number check each number and print if it is prime.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.util.Scanner; public class PrimeNumberChecker2 { public static void main(String[] args) { System.out.println("Enter a number range to generate prime numbers in between"); Scanner scanner = new Scanner(System.in); int number1 = scanner.nextInt(); int number2 = scanner.nextInt(); if (number1 >= number2) { System.out.println("Number2 must be greater then number1"); System.exit(0); } while (number1 <= number2) { int i = 2, count = 0; while (i <= number1 / 2) { if (number1 % i == 0) { count++; break; } i++; } if (count == 0) { System.out.println(number1 + " is prime number"); } number1++; } } } |

Output

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Enter a number range to generate prime numbers in between 1 50 1 is prime number 2 is prime number 3 is prime number 5 is prime number 7 is prime number 11 is prime number 13 is prime number 17 is prime number 19 is prime number 23 is prime number 29 is prime number 31 is prime number 37 is prime number 41 is prime number 43 is prime number 47 is prime number |

print prime numbers in java in specified range also checks that number2 must be greater than number1.

## 5 Prime number between given range in java

Prime number java program in range takes two integer range and check each number is prime or not if it is prime then prime number method returns list of prime numbers to calling method.

java program for prime numbers code is same as above code we have only created function by using that code.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class PrimeNumberChecker3 { public static List<Integer> generatePrimeInRange(int startRange, int endRange) { if (startRange >= endRange) { System.out.println("endRange must be greater then startRange"); System.exit(0); } List<Integer> primeList = new ArrayList<>(); while (startRange <= endRange) { int i = 2, count = 0; while (i <= startRange / 2) { if (startRange % i == 0) { count++; break; } i++; } if (count == 0) { primeList.add(startRange); } startRange++; } return primeList; } public static void main(String[] args) { System.out.println("Enter a number range to generate prime numbers in between"); Scanner scanner = new Scanner(System.in); int number1 = scanner.nextInt(); int number2 = scanner.nextInt(); List<Integer> primeNumbers = generatePrimeInRange(number1, number2); System.out.println("Prime Numbers are"); System.out.println(primeNumbers); } } |

Output

1 2 3 4 5 | Enter a number range to generate prime numbers in between 1 100 Prime Numbers are [1, 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] |

In above programs we have seen different java program to print prime numbers.

Hope you learned easy way to how to check prime number in java.