Write a method which takes as input an integer, returns true if the integer is prime, and returns false otherwise. Do not import anything.
If the input is negative, 0 or 1, false should be returned.
If the input x is greater than 1, you can test if it is prime (inefficiently) by checking if it is divisible by any integer from 2 up to half of x. If it is not divisible by any of these numbers, then it is prime; otherwise, it is not prime.
Your submission should also include the client that you use to test your method.
in java
//IsPrimeCheck.java import java.util.Scanner; public class IsPrimeCheck { // Returns true if x is prime, and false otherwise. public static boolean isPrime(int x) { if(x<2){ return false; } else { for (int i = 2; i <= x/2; i++) { if (x % i == 0) { return false; } } return true; } } public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Please enter an integer"); int n = scan.nextInt(); System.out.println(isPrime(n)); } }
Get Answers For Free
Most questions answered within 1 hours.