As we know that prime number is a natural number which is greater than 1 and divisible by 1 and itself. So it means that it has only two factors. We are going to use this logic to check whether number is prime or not.
Solution:-
public class primeNumber {
public static void main(String[] args) {
int number=19;
int factor=0;
if(number>1){
for (int i=0; i<=number; i++){
if (number%b==0){
factor++;
}
}
}
if (factor==2){
System.out.println("Given number is prime number");
}else if (factor>2){
System.out.println("Given number is non prime number");
}
}
}
0 comments:
Post a Comment