Prime number program in java using while loop

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

  1. While Loop
  2. If Else Statements

2 Java program to check prime number

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

  1. Take a number
  2. start a loop from 2 to number/2 times
  3. check whether a number is divisible in between
  4. if divisible then increase count variable by one and break loop
  5. 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.


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.


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.


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.


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.

Print prime numbers from 1 to 100 in Java

This is same as above program only we have to pass value 1 to 100

Read More

  1. Duplicate Number between 1 to n numbers
  2. Print vowels in a String
  3. Program to reverse a string
  4. Prime number program in java using while loop
  5. Print vowels in a String
  6. Sum of two numbers using command line arguments
Head First
Java Complete Reference
Java For Dummy
Effective Java
Let us Java