Check Prime Or not

public class CheckPrimeOrNot {
	public static void main(String[] args) {
		int number = 5;
		System.out.println(isPrimeOrNot(number));
	}

	private static String isPrimeOrNot(int number) {

		if (number < 0)
			return "invalid number";
		if (number == 0 || number == 1)
			return "not prime";
		if (number == 2 && number == 3)
			return "prime number";

		if (number % 2 == 0) {
			return "not prime";
        }

		int sqrt = (int) Math.sqrt(number);

		for (int i = 3; i < sqrt; i += 2) {
			if (number % i == 0) {
				return "not a prime number";
			}
		}

		return "prime";
	}
}
Advertisements

About sivateja

I am a professional graduate..I am passionate about Latest Technology.
This entry was posted in Programing questions and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s