Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming Tips & Tricks»Java Program to Check Prime Number

    Java Program to Check Prime Number

    By RahulJune 17, 20233 Mins Read

    A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. In simpler terms, a prime number is a number that has only two distinct natural number divisors: 1 and itself. This property makes prime numbers an interesting and widely studied topic in the world of mathematics and computer science.

    In this article, we will learn how to write a Java program that checks whether a given number is a prime number.

    Java Program to Check Prime Number

    The key idea to check whether a number is prime or not is to iterate from 2 to the square root of the number and check if the number is divisible by any number in this range. If it is, then it is not a prime number. We can iterate up to the square root of the number because a larger factor of the number must be a multiple of a smaller factor that has already been checked.

    Let’s see how we can implement this in Java:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    public class Main {
        public static void main(String[] args) {
            int num = 29;
            boolean isPrime = checkPrime(num);
            if(isPrime) {
                System.out.println(num + " is a prime number.");
            } else {
                System.out.println(num + " is not a prime number.");
            }
        }
     
        public static boolean checkPrime(int num) {
            if (num <= 1) {
                return false;
            }
            for (int i = 2; i <= Math.sqrt(num); i++) {
                if (num % i == 0) {
                    return false;
                }
            }
            return true;
        }
    }

    In the above code:

    1. We have a main method where we initialize the number num we want to check.
    2. We call the checkPrime method, passing num as an argument.
    3. In the checkPrime method, we first check if num is less than or equal to 1. If it is, we immediately return false as 1 is not considered a prime number.
    4. Then we run a loop that goes from 2 to the square root of num. For each number in this range, we check if num is divisible by this number (num % i == 0). If it is, that means num has a divisor other than 1 and itself, so we return false.
    5. If we have checked all numbers up to the square root of num and found no divisors, we return true, indicating that num is a prime number.
    6. Back in the main method, we print out whether num is a prime number based on the result of the checkPrime method.

    When you run this program with num = 29, the output will be:

    1
    29 is a prime number.

    If you change the value of num to 15, the output will be:

    1
    15 is not a prime number.

    This simple Java program is an efficient way to check whether a number is prime or not, making use of the mathematical property that a number is prime if it has no divisors other than 1 and itself.

    Conclusion

    Writing a Java program to check whether a number is prime or not is an excellent exercise for developing problem-solving skills and understanding core programming concepts. It involves the application of simple mathematical concepts and basic control structures in Java like loops and conditional statements.

    We’ve seen how to implement a prime number checking algorithm efficiently, i.e., by iterating only up to the square root of the number. This reduces the time complexity significantly, especially when working with larger numbers.

    Remember that practical problem-solving exercises like these form the foundation of good programming skills. Don’t hesitate to experiment with different approaches and try to optimize your solutions even further. Keep coding and keep learning!

    Java Prime Number
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to Read InputStream into a String in Java

    Java Program to Reverse a String

    Java Program to Reverse a Number

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Create and Use Custom Python Module
    • How to Install and Use Podman on Ubuntu 22.04 & 20.04
    • Setting Up Laravel with Docker and Docker-compose
    • Setting Up Development Environments with PHP and Docker
    • Using Composer with Different PHP Versions in Linux
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.