Prime number in Java
To watch in YouTube: click here
What is a prime number ?
A prime number is a number that is greater than 1 and has only two factors: 1 and the number itself.
That means, if you divide the number by any number other than 1 or the number itself, the remainder will not be zero.
For example:
2, 3, 5, 7, 11, 13
package testing;
public class TestPrimeNumber {
public static void main(String[] args) {
int givenNum = 12;
boolean isPrimeNumber = true ;
for(int i=2; i<givenNum; i++) {
if(givenNum % 2 == 0) {
isPrimeNumber = false ;
break ;
}
}
System.out.println("Is the given num a prime number ? "+isPrimeNumber) ;
}
}
Output :
givenNum = 12;
Is the given num a prime number ? false
givenNum = 11;
Is the given num a prime number ? true
No comments:
Post a Comment