Find whether number is prime or not


import java.util.Scanner;

public class IsPrime {
	public static void main(String args[]){
		System.out.println("Enter a number");
		Scanner sc = new Scanner(System.in);
		int number=sc.nextInt();
		if(isPrime(number)){
			System.out.println("Number is Prime");
		}else{
			System.out.println("Number is not Prime");
		}
	}

	private static boolean isPrime(int number) {
		if(number<2)return false;
		for(int i=2;i<=Math.sqrt(number);i++){
			if(number%i==0) return false;
		}
		return true;
	}
}

Leave a Reply

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