#### Advertise with Us!

We have a variety of advertising options which would give your courses an instant visibility to a very large set of developers, designers and data scientists.

View Plans## Table of Contents

One of the most frequently asked questions that any Java developer needs to have the answer to is how to write a prime number program in Java. It is one of the basic concepts concerning the leading high-level, general-purpose programming language.

There are several ways of writing a program in Java that checks whether a number is prime on not. However, the basic logic remains the same i.e. you need to check whether the entered number (or already defined in the program) has some divisor other than 1 and itself or not.

The prime number program is an indispensable part of learning Java. Hence, most of the great books on Java covers it. Before moving forward to discuss the prime number program in Java, let’s first understand the concept of prime numbers and their importance.

**Prime Numbers – The Definition and Importance**

Any number that is only divisible by 1 other than itself is known as a primary number. 3, 5, 23, 47, 241, 1009 are all examples of prime numbers. While 0 and 1 can’t qualify for being a prime number, 2 is the only even prime number in the entire infinitely long set of prime numbers.

Prime numbers exhibit a number of odd mathematical properties that make them desirable for a wide variety of applications, many of which belongs to the world of information technology. For example, primes find use in pseudorandom number generators and computer hash tables.

There are several instances in the history of using encryption for hiding information in plain sight. Amazingly, it is the process of using prime numbers to encode information.

With the introduction of computers, modern cryptography was introduced. It became feasible to generate complex and longer codes that were much, much difficult to crack.

Most of the modern computer cryptography is dependent on making use of prime factors of large numbers. As prime numbers are the building blocks of whole numbers, they are of the highest importance to number theorists as well.

Check out more about the importance of prime number in IT security.

**Prime Number Program in Java**

As already mentioned, there are several ways of implementing a prime number program in Java. In this section, we’ll look at three separate ways of doing so as well as 2 additional programs for printing primes.

**Type 1 – A Simple Program With No Provision for Input**

This is one of the simplest ways of implementing a program for checking whether a number is a prime number or not in Java. It doesn’t require any input and simply tells whether the defined number (by the integer variable n) is a prime number or not. Here goes the code:

public class PrimeCheck{ public static void main(String args[]){ int i,m=0,flag=0; int n=3; m=n/2; if(n==0||n==1){ System.out.println(n+" is not a prime number."); } else{ for(i=2;i<=m;i++){ if(n%i==0){ System.out.println(n+" is not a prime number."); flag=1; break; } } if(flag==0) { System.out.println(n+" is a prime number."); } } } }

**Output:**

3 is a prime number.

**Type 2 – A Program in Java Using Method (No User Input Required)**

This Java code demonstrates the implementation of a prime number program that uses a method. Like the program mentioned before, it doesn’t ask for any user input and works only on the numbers entered to the defined method (named checkPrime) in the program. Here is the code:

public class PrimeCheckUsingMethod{ static void checkPrime(int n){ int i,m=0,flag=0; m=n/2; if(n==0||n==1){ System.out.println(n+" is not a prime number."); }else{ for(i=2;i<=m;i++){ if(n%i==0){ System.out.println(n+" is not a prime number."); flag=1; break; } } if(flag==0) { System.out.println(n+" is a prime number."); } } } public static void main(String args[]){ checkPrime(1); checkPrime(3); checkPrime(17); checkPrime(20); } }

**Output:**

1 is not a prime number.

3 is a prime number.

17 is a prime number.

20 is not a prime number.

**Type 3 – A Program in Java Using Method (Requires User Input)**

This Java program is similar to the aforementioned program. However, this program prompts for user input. Here goes the code:

import java.util.Scanner; import java.util.Scanner; public class PrimeCheckUsingMethod2 { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Enter a number: "); int n = s.nextInt(); if (isPrime(n)) { System.out.println(n + " is a prime number."); } else { System.out.println(n + " is not a prime number."); } } public static boolean isPrime(int n) { if (n <= 1) { return false; } for (int i = 2; i < Math.sqrt(n); i++) { if (n % i == 0) { return false; } } return true; } } )

**Sample Output:**

Enter a number: 22

22 is not a prime number.

**[Bonus Program] Type 4 – A Program in Java to Print Prime Numbers from 1 to 100**

This code will demonstrate a Java program capable of printing all the prime numbers existing between 1 and 100. The code for the program is:

class PrimeNumbers { public static void main (String[] args) { int i =0; int num =0; String primeNumbers = ""; for (i = 1; i <= 100; i++) { int counter=0; for(num =i; num>=1; num--) { if(i%num==0) { counter = counter + 1; } } if (counter ==2) { primeNumbers = primeNumbers + i + " "; } } System.out.println("Prime numbers between 1 and 100 are :"\n); System.out.println(primeNumbers); } }

**Output:**

Prime numbers between 1 and 100 are:

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

**[Bonus Program] Type 5 – A Program in Java to Print Prime Numbers from 1 to n (User Input)**

This Java program prints all the prime numbers existing between 1 and n, where n is the number entered by the user. Here is the code:

import java.util.Scanner; class PrimeNumbers2 { public static void main (String[] args) { Scanner scanner = new Scanner(System.in); int i =0; int num =0; String primeNumbers = ""; System.out.println("Enter a number:"); int n = scanner.nextInt(); scanner.close(); for (i = 1; i <= n; i++) { int counter=0; for(num =i; num>=1; num--) { if(i%num==0) { counter = counter + 1; } } if (counter ==2) { primeNumbers = primeNumbers + i + " "; } } System.out.println("Prime numbers between 1 and n are:"/n); System.out.println(primeNumbers); } }

**Sample Output:**

Enter a number: 22

Prime numbers between 1 and 22 are:

2 3 5 7 11 13 17 19

**It’s Done!**

That was all about the prime number program in Java. No matter at what skill level a Java developer is, it is very important to be able to write a program concerning prime numbers, at least for checking whether a given number (or set of numbers) is a prime or not.

Continued learning is very important for advancing in coding. A program that you are able to write now might be better when you write it after gaining new knowledge. If you’re looking to enhance your Java skill further, consider checking out some of the best Java tutorials.

Do you know some fascinating way of implementing a prime number program in Java? Care to share with us? Then you can do so by the comment window below. Thanks in advance.

**People are also reading:**

## Leave a comment