Print prime numbers in java
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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
Content posted is based on learning or working experience,
Share the post with others if find useful,
Any queries?, leave comments or discuss on our facebook:qavalidation.com, Thanks!