Find your Math Personality!
Math & Beyond

Prime Number Program in Java

12k views

26 January 2021

Reading Time: 4 minutes

Learn how to check whether a number is prime or not using the Java program.


What Are Prime Numbers?

Positive numbers which are numbers including and greater than 1 are known as natural numbers. Prime numbers are a special type of natural numbers. 

The specialty of prime numbers lies in the fact that prime numbers are not divisible by any number apart from 1 and themselves. 

This means no numbers apart from 1, and the number itself can be multiplied with each other to obtain the number in question.

An example of a prime number is 7. The only pair of numbers that can be multiplied with each other to obtain the number 7 is 1, and 7. 1 and 7 are hence the factors of the number 7. 

Numbers that are not prime, and have more than two factors are known as composite numbers or composites. Primality is the property of a number that indicates whether it is prime or not. It was demonstrated by Euclid using Euclid’s Theorem that an endless number of prime numbers exist. 

These numbers are nearly randomly distributed throughout the array of natural numbers. However, the probability that a number is prime is inversely proportional to the number of digits it has.

But why study these numbers separately? Why do we want to make a special program for prime numbers? In the real world, prime numbers have tons and tons of applications, in information technology and outside of it. 

The main computer science application of prime numbers can be found in the form of cryptography. Scores of cryptography algorithms have their foundations in prime numbers of high orders. 

Prime numbers also form the basis of hash tables and checksum methods. Other applications of prime numbers are in mathematics, quantum mechanics as well as biology.


Prime Number Program in Java

Program to check whether the given number is prime or not? How to display prime numbers using Java Code. The main method contains a loop to check prime numbers one by one. Prime Number Program in Java is mentioned below in the Downloadable PDF. 

📥 Prime Number Program in Java

Download


How to Check if a Number is Prime in Java?

Java is an object-oriented programming language developed by Oracle. It is an extremely popular language, especially for beginners to programming, since it has very few dependencies. 

As with most other blocks of code, the prime number program in Java is also bound to follow a particular algorithm.

The algorithm of the prime number program in Java is based on an age-old method to find whether a number is prime or not. Two basic methods can distinguish prime numbers in Java.

The first method, followed by the prime number program in Java is to check whether the remainder of the number when it is divided by each number up to half of itself is greater than 0. 

The second method to distinguish prime numbers in Java is to check whether the remainder of the number when it is divided by each number from 2 to its square root is greater than zero. 

This follows the fact that the remainder of a number when it is divided by one of its factors is zero, and no factor can be greater than its square root.

Here is the list of steps to be followed to build a prime number program in Java.

  • Take the input of the number to check if a number is prime in Java.
     
  • Once the input has been taken, declare a variable that represents the divisors. This variable will be incremented, and the input will be divided by it at every step. Initialize this with the value 2.
     
  • Declare a boolean with the value false. If a divisor is found, the boolean will be converted to true and the program will output the result.
     
  • Define a for loop using the divisor, from the divisor value 2 to half of the input number. Increment the divisor by 1 on every step.
     
  • Inside the for loop, define a conditional statement that exits the loop by changing the value of the boolean if the remainder between the input number and the divisor is found to be zero.
     
  • Outside the loop, define a conditional statement that tells if the number is prime or not on the basis of the boolean value.
     
  • You can also develop the same program using a while loop, that increments the value of the divisor by 1 while the value of the boolean is false.

Java Programs for Finding out Prime Number

The following is the program for prime number in Java as per the previous rules using both the for loop and the while loop.

Prime Number Program in Java Using For Loop

public class Main {

    public static void main(String[] args) {

        int num = 29;
        boolean flag = false;
        for(int i = 2; i <= num/2; ++i)
        {
            if(num % i == 0)
            {
                flag = true;
                break;
            }
        }

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


Prime Number Program in Java Using While Loop

public class Main {

    public static void main(String[] args) {

        int num = 33, i = 2;
        boolean flag = false;
        while(i <= num/2)
        {
            if(num % i == 0)
            {
                flag = true;
                break;
            }

            ++i;
        }

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


Conclusion

Prime numbers are an inseparable part of mathematics, and every programmer needs to know how to distinguish prime numbers in Java. The prime number program in Java is hence among the first programs that are taught to beginners. This text provided a brief on how to develop a program for prime number in Java. To know more, and if you have any questions, do not forget to comment! 

About Cuemath

Cuemath, a student-friendly mathematics and coding platform, conducts regular Online Classes for academics and skill-development, and their Mental Math App, on both iOS and Android, is a one-stop solution for kids to develop multiple skills. Understand the Cuemath Fee structure and sign up for a free trial.


Frequently Asked Questions (FAQs)

What are co-prime numbers?

Pair of Numbers that have their greatest common divisor i.e. GCD is unity, which means that the only number that can divide each of them is 1.

How to find prime numbers?

Prime Numbers can be found by using the Sieve of Eratosthenes.

How many prime numbers are from 1-100?

There are 25 prime numbers from 1-100.

What are twin prime numbers?

The pair of primes that differ by an amount of 2 are known as twin prime numbers.

What is the sieve of Eratosthenes?

It is an algorithm to find all given numbers up to a limit.


Related Articles