Java 8 Program To Find Prime Number

Introduction

A prime number is a natural number greater than 1 that has no divisors other than 1 and itself. This guide will show you how to create a Java program that checks whether a given number is prime using Java 8 features.

Problem Statement

Create a Java program that:

  • Takes an integer input from the user.
  • Checks if the number is prime using Java 8 Streams.
  • Returns and displays whether the number is prime.

Example 1:

  • Input: 7
  • Output: 7 is a prime number.

Example 2:

  • Input: 10
  • Output: 10 is not a prime number.

Solution Steps

  1. Prompt for Input: Use the Scanner class to read an integer input from the user.
  2. Use Java 8 Streams to Check for Primality:
    • Use a stream of numbers from 2 to the square root of the given number.
    • Check if any of these numbers divide the given number without a remainder.
  3. Display the Result: Print whether the number is prime or not.

Java Program

Java 8 Program to Find Prime Numbers

import java.util.Scanner;
import java.util.stream.IntStream;

/**
 * Java 8 Program to Check if a Number is Prime
 * Author: https://www.javaguides.net/
 */
public class PrimeNumberCheck {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Step 1: Prompt the user for input
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();

        // Step 2: Check if the number is prime using Java 8 streams
        boolean isPrime = isPrime(number);

        // Step 3: Display the result
        if (isPrime) {
            System.out.println(number + " is a prime number.");
        } else {
            System.out.println(number + " is not a prime number.");
        }
    }

    // Method to check if a number is prime using Java 8 streams
    public static boolean isPrime(int number) {
        if (number <= 1) {
            return false; // Numbers less than or equal to 1 are not prime
        }
        return IntStream.rangeClosed(2, (int) Math.sqrt(number))  // Stream of numbers from 2 to sqrt(number)
                .noneMatch(divisor -> number % divisor == 0);    // Check if any number divides the input
    }
}

Explanation

  • Input: The program prompts the user to enter an integer.

  • Checking for Primality Using Streams:

    • The isPrime() method first checks if the number is less than or equal to 1, which is not prime.
    • The IntStream.rangeClosed(2, (int) Math.sqrt(number)) generates a stream of numbers from 2 to the square root of the number.
    • The noneMatch() method checks if none of these numbers divide the input number evenly (number % divisor == 0), meaning the number is prime if none of these conditions hold true.
  • Output: The program prints whether the number is prime.

Output Example

Example 1:

Enter a number: 7
7 is a prime number.

Example 2:

Enter a number: 10
10 is not a prime number.

Explanation of Output:

  • Example 1: The number 7 is checked against numbers from 2 to the square root of 7 (which is approximately 2.6). Since none of these numbers divide 7 evenly, the program correctly identifies 7 as a prime number.
  • Example 2: The number 10 is divisible by 2 and 5, so the program correctly identifies 10 as not a prime number.

Conclusion

This Java 8 program efficiently checks if a number is prime using streams. By leveraging Java 8's IntStream and noneMatch() methods, the program provides a concise and efficient way to determine the primality of a number. This approach is useful in scenarios where you need to quickly and effectively check for prime numbers.

Comments