Explore Prime Number Program in Java with 4 Examples

A Prime number is a very crucial number that plays an important role in the concept of mathematics and computers. It is a natural number that is greater than 1 and has only 2 factors which are 1 and the number itself. Let’s see the Prime number program in Java.

Don’t know Java Well? No worries, Have a look at our Java Free Course.

Prime number program in Java

Prime Number Program in Java

Prime number program in Java: A prime number is a number which has only 2 factors, and those are 1 and the number itself. So, if the number is divisible by any other number then it is not a prime number. The smallest Prime number is 2. 1 is not a prime number.

Example 1: Prime number program in Java using while loop

public class Main {
public static void main(String[] args) {
int num = 12;

// count variable is used to count the number of factors for a given number
int count = 0;

int i = 1; // for initiating while loop

while (i<=num) {
if (num%i == 0) { // this condition will check whether the number is divisible or not
count = count+1; // counting the factors
}
i=i+1;
}

// comparing if the number of factors is equal to 2 or not
if (count == 2){
System.out.println (num+" is a prime number");
}
else {
System.out.println (num+" is not a prime number");
}
}
}

The output of the Example 1 will be like this:

Prime number program in Java using while loop

Example 2: Prime number program in Java using for loop

public class Main {
public static void main(String[] args) {
int num = 11;

// count variable is used to count the number of factors for a given number
int count = 0;

for (int i=1; i<=num; i++) {
if (num%i == 0) { // this condition will check whether the number is divisible or not
count = count+1; // counting the factors
}
i=i+1;
}

// comparing if the number of factors is equal to 2 or not
if (count == 2){
System.out.println (num+" is a prime number");
}
else {
System.out.println (num+" is not a prime number");
}
}
}

The output of the Example 2 will be like this:

Example 2: Prime number program in Java using for loop

Example 3: Prime number program in Java using Method/Functions

public class Main {
// Methode declared for checking whether the number is prime or not
public static void primeChecker(int num){

// count variable is used to count the number of factors for a given number
int count = 0;

for (int i=1; i<=num; i++) {
if (num%i == 0) { // this condition will check whether the number is divisible or not
count = count+1; // counting the factors
}
i=i+1;
}

// comparing if the number of factors is equal to 2 or not
if (count == 2){
System.out.println (num+" is a prime number");
}
else {
System.out.println (num+" is not a prime number");
}
}
public static void main(String[] args) {
int num = 17;

primeChecker (num); // Method Called

}
}

The output of the Example 3 will be like this:

Example 3: Prime number program in Java using Method/Functions

Example 4: Prime number program in Java using Method/Functions (Another Way)

public class Main {
// Methode declared for checking whether the number is prime or not
public static boolean primeChecker(int num){

// count variable is used to count the number of factors for a given number
int count = 0;

for (int i=1; i<=num; i++) {
if (num%i == 0) { // this condition will check whether the number is divisible or not
count = count+1; // counting the factors
}
i=i+1;
}

// comparing if the number of factors is equal to 2 or not
if (count == 2){
return true; // return true if the number is prime
}
else {
return false;
}
}
public static void main(String[] args) {
int num = 23;

if (primeChecker (num)) {
System.out.println (num+" is a prime number");
}
else {
System.out.println (num+" is not a prime number");
}

}
}

The output of the Example 4 will be like this:

Example 4: Prime number program in Java using Method/Functions (Another Way)

Conclusion

One of the most important programs in Java is the Prime number program in Java. In this article, 4 different ways have been demonstrated for the same program and those are withe while loops, for loops,  with user-defined functions.