Java Program To Check Whether Number is Prime Or Not
==================================================
// What is Prime Number?
// A number which is divisible by 1 and itself only then that number is
// considered to be Prime Number
// Example: 1) 3 is divisible by 1 and 3 only(It is having only two factors.)
import java.util.Scanner;
public class PrimeNumberOrNot {
public static void main(String[] args) {
// What is Prime Number?
// A number which is divisible by 1 and itself only then that number is
// considered to be Prime Number
// Example: 1) 3 is divisible by 1 and 3 only(It is having only two factors.)
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Number for checking out whether it is prime or not:");
Integer number = sc.nextInt();
// 0 and 1 are not a prime Numbers.
Integer remainder = 0, count = 0;
for (int i = 1; i <= number; i++) {
remainder = number % i;
if (remainder == 0) {
count++;
}
}
if (count == 2) {
System.out.println(number + " is a prime Number");
} else {
System.out.println(number + " is not a prime number");
}
}
}
Output:
==========
Enter the Number for checking out whether it is prime or not:
79
79 is a prime Number
Comments
Post a Comment