Program: Write a program to check the given number is a prime number or not?

Description:

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. A
natural number greater than 1 that is not a prime number is called a composite number. For example, 5 is prime, as only 1 and 5 divide it,
whereas 6 is composite, since it has the divisors 2 and 3 in addition to 1 and 6. The fundamental theorem of arithmetic establishes the
central role of primes in number theory: any integer greater than 1 can be expressed as a product of primes that is unique up to ordering.
This theorem requires excluding 1 as a prime.

Code:

package com.java2novice.algos;
public class MyPrimeNumCheck {
public boolean isPrimeNumber(int number){
for(int i=2; i<=number/2; i++){
if(number % i == 0){
return false;
}
}
return true;
}
public static void main(String a[]){
MyPrimeNumCheck mpc = new MyPrimeNumCheck();
System.out.println("Is 17 prime number? "+mpc.isPrimeNumber(17));
System.out.println("Is 19 prime number? "+mpc.isPrimeNumber(19));
System.out.println("Is 15 prime number? "+mpc.isPrimeNumber(15));
}
}

Output:

Is 17 prime number? true
Is 19 prime number? true
Is 15 prime number? false

Class is a template for multiple objects with similar features and it is a blue print for objects. It defines a type of object
according to the data the object can hold and the operations the object can perform. Constructor is a special kind of method that
determines how an object is initialized when created. Primitive data types are 8 types and they are: byte, short, int, long, float,
double, boolean, char.

Famous Quotations

We know what we are, but know not what we may be.

-- William Shakespeare

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All
examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any
mistakes or bugs, please email me to [email protected] or you can comment on the page.