Java
Java

 

 

Introduction

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In other words, a prime number is only divisible by 1 and the number itself. For example, the numbers 2, 3, 5, 7, and 11 are prime numbers.

Objective

The objective of this program is to determine whether a given number is prime or not. This will help users understand the concept of prime numbers and provide a practical application of programming logic and algorithms.

Java Code

public class PrimeChecker {
    public static void main(String[] args) {
        int number = 29; // You can change this number to test other values
        boolean isPrime = checkPrime(number);
        
        if (isPrime) {
            System.out.println(number + " is a prime number.");
        } else {
            System.out.println(number + " is not a prime number.");
        }
    }
    
    public static boolean checkPrime(int num) {
        if (num <= 1) {
            return false; // 0 and 1 are not prime numbers
        }
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0) {
                return false; // Found a divisor, not prime
            }
        }
        return true; // No divisors found, it's prime
    }
}

Program Structure and Execution

This Java program consists of a main class named PrimeChecker with two methods:

  • main: The entry point of the program, where the number to be checked is defined. It calls the checkPrime method and prints the result.
  • checkPrime: This method takes an integer as an input and checks if it is prime by testing for divisibility from 2 up to the square root of the number. It returns true if the number is prime and false otherwise.

How to Run the Program

  1. Make sure you have Java Development Kit (JDK) installed on your machine.
  2. Create a new file named PrimeChecker.java and copy the code provided above into this file.
  3. Open a command prompt or terminal window.
  4. Navigate to the directory where you saved PrimeChecker.java.
  5. Compile the program using the command: javac PrimeChecker.java
  6. Run the compiled program using the command: java PrimeChecker
  7. Observe the output, which will indicate whether the defined number is prime.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)