## Print prime numbers from 1 to n in java

Print prime numbers from 1 to n in java using while loop

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.util.Scanner; public class PrimeNumberChecker { public static void main(String[] args) { System.out.println("Enter a number n"); Scanner scanner = new Scanner(System.in); int number1 = 1; int number2 = scanner.nextInt(); if (number1 >= number2) { System.out.println("Number2 must be greater then number1"); System.exit(0); } while (number1 <= number2) { int i = 2, count = 0; while (i <= number1 / 2) { if (number1 % i == 0) { count++; break; } i++; } if (count == 0) { System.out.println(number1 + " is prime number"); } number1++; } } } |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | Enter a number n 100 1 is prime number 2 is prime number 3 is prime number 5 is prime number 7 is prime number 11 is prime number 13 is prime number 17 is prime number 19 is prime number 23 is prime number 29 is prime number 31 is prime number 37 is prime number 41 is prime number 43 is prime number 47 is prime number 53 is prime number 59 is prime number 61 is prime number 67 is prime number 71 is prime number 73 is prime number 79 is prime number 83 is prime number 89 is prime number 97 is prime number |

Read More Prime Number Program In Java Using While Loop