A prime number which can not be divided by any other number except 1 or by itself.
- The only even prime number is 2. All other even numbers can be divided by 2.
- Zero and 1 are not considered prime numbers.
e.g –
3 – prime number (3 can not be divided by any other number but can be divided by 1 or itself)
9 – not a prime number (can be divided by 3)
Let’s see the implementation
public class PrimeNo { public static void main(String[] args) { int max = 100; System.out.println("Prime numbers are - "); for(int no = 2 ; no <= max ; no++) { boolean isPrm = true; for(int i = 2; i <= no/2 ; i++) { if(no % i == 0){ isPrm = false; break; } } if(isPrm){ System.out.print(no+" "); } } } }
OutPut:
Prime numbers are –
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97