Check if a number is prime

Report a typo

You need to implement the isPrime method to check whether the input number is prime or not.

It's guaranteed that input value is always greater than 1 (i.e. 2, 3, 4, 5, ....). Use the provided template for your method.

A prime number is a value greater than 1 that has no positive divisors other than 1 and itself. More details are here.

This problem has a simple and clear solution with streams. Please, do not use loops. In order to create a stream of numbers you can look into Stream.iterate() or IntStream.range().

Sample Input 1:

2

Sample Output 1:

True

Sample Input 2:

3

Sample Output 2:

True

Sample Input 3:

4

Sample Output 3:

False
Write a program in Java 17
import java.util.Scanner;
import java.util.stream.*;

class PrimeNumbers {

/**
* Checking if a number is prime
*
* @param number to test >= 2
* @return true if number is prime else false
*/
private static boolean isPrime(long number) {
// write your code here
return false;
}

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

String line = scanner.nextLine().trim();

int n = Integer.parseInt(line);

System.out.println(isPrime(n) ? "True" : "False");
}
}
___

Create a free account to access the full topic