While Loop Print Prime Numbers In Java

1. Overview


In this article, You'll learn how to use a while loop to print prime numbers in java. This is a very basic programming interview question for the freshers. First, we'll talk about what is prime number?.

A prime number (or a prime) is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. 

Note:

A natural number greater than 1 that is not prime is called a composite number.

While Loop Print Prime Numbers In Java


2. While Loop


While loop is a loop that controls the program execution by specifying the condition. It runs the same logic untill the given condition is satisfied.

2.1 Syntax:


while(condition){
// logic to run

//increment or decrement statement
}


2.2 Example to print 1 to 10 numbers:


package com.java.w3schools.blog.loop;

public class WhileLoopExample {

public static void main(String[] args) {

int i = 1;
while(i <=10){
System.out.println(i);
i++;
}
}
}

Output:

1
2
3
4
5
6
7
8
9
10

Read and Understand While Loop & Nested While Loop.

3. Example to print prime numbers from 1 to 100 (1 to N)


package com.java.w3schools.blog.printnumbers;

public class PrimeNumbersExample {

public static void main(String[] args) {

int primeCheckNumber;
int n = 1;
int divisibleCount;

while (n <= 100) {
divisibleCount = 0;
primeCheckNumber = 2;
while (primeCheckNumber <= n / 2) {
if (n % primeCheckNumber == 0) {
divisibleCount++;
break;
}
primeCheckNumber++;
}
if (divisibleCount == 0 && n != 1) {
System.out.print(n + " ");
}
n++;
}

}
}

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 


This program uses the two while loops. First, while loop to run numbers from 1 to 100 and second while loop is to check the current number is prime or not. If any number is divisible then divisibleCount value will be incremented by 1. If and only if divisibleCount == 0 then it is said to be a prime number.

4. Checking given number is prime or not using while loop


In the above program, I learned how to print prime numbers using a while loop. But now, you will learn how to check the given number is prime or not. Logic is very simple. Inner while loop logic is the core logic to find the number is prime or not.

package com.java.w3schools.blog.printnumbers;

public class CheckPrimeWhileLoop {

public static void main(String[] args) {

int givenNumber = 31;
int n = 2;
int divisibleCount = 0;;
while (n <= givenNumber / 2) {
if (givenNumber % n == 0) {
divisibleCount++;
break;
}
n++;
}
if (divisibleCount == 0 ) {
System.out.print(givenNumber + " is a prime number ");
} else {
System.out.print(givenNumber + " is not a prime number ");
}

}
}

Output :

31 is a prime number 

5. Conclusion


In this article, We've seen how to print prime numbers in java using while loop.

Covered in this post.


  • What is a Prime Number?
  • What is a while loop?
  • While Loop Syntax
  • Example of while loop
  • Java program to print prime numbers from 1 to N using a while loop.
  • Java program to check the given number is prime or not.


If you have questions please post in comments. I will reply to all your queries. Share with friends.

0 Comments