Java Program for Prime Number

Java program for prime number
Reading Time: 4 minutes

Introduction:

The provided Java program is designed to determine whether a given number is a prime number or not. Prime numbers are natural numbers greater than 1 that are divisible only by 1 and themselves. The program utilizes a simple algorithm to check the primality of the input number.

Explanation:

The program consists of a PrimeChecker class with a main method that serves as the entry point. It takes user input through a Scanner to obtain a number and then calls the isPrime method to check if the entered number is prime. The result is then displayed to the user.

The isPrime method is a separate function responsible for the primality check. It returns a boolean value—true if the number is prime and false otherwise. The method first checks if the input number is less than or equal to 1, in which case it immediately returns false since prime numbers must be greater than 1.

Next, a loop runs from 2 to the square root of the input number. During each iteration, it checks if the number is divisible by the current iteration index. If the number is divisible by any value in this range, the method concludes that the number is not prime and returns false. If no divisor is found, the number is considered prime, and the method returns true.

Algorithm:

  1. Begin the program by prompting the user to enter a number.
  2. Read the input number using the Scanner
  3. Call the isPrime method, passing the entered number as an argument.
  4. Inside the isPrime method:
    • If the input number is less than or equal to 1, return false.
    • Iterate from 2 to the square root of the input number.
    • If the input number is divisible by any value in the iteration range, return false.
    • If no divisor is found, return true.
  5. Display the result of the primality check in the main

The program provides a straightforward way to determine whether a given number is a prime number using a common primality testing algorithm.

Code:

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

    
   

Input :

    
     Enter a number: 6
    
   

Output :

    
     6 is not a prime number.
    
   

Conclusion :

This program takes a number as input and uses a simple loop to check whether it is divisible by any number from 2 to the square root of the given number. If it is divisible by any number, it’s not prime; otherwise, it’s prime.

FAQ :

Q1: How can I write a Java program for prime numbers?

A1: To create a Java program for prime numbers, you can use a simple algorithm to check whether a given number is prime or not.

Q2: What is the syntax for a Java program of prime numbers?

A2: The syntax for a Java program of prime numbers involves using loops and conditionals to determine if a given number is prime or not.

Q3: Can you provide an example of a Java program for prime numbers?

A3: Certainly! You can use nested loops and the modulus operator to implement a Java program to check if a number is prime or not.

Q4: How do I use a Java program to check if a number is prime or not?

A4: You can use a Java program to check if a number is prime by iterating through potential divisors and checking for any factors other than 1 and the number itself.

Q5: Is there a specific Java program to determine if a number is prime or not?

A5: Yes, you can write a Java program specifically designed to check whether a given number is prime or not by implementing a reliable prime-checking algorithm.