###### Print Prime numbers up to given number.

This article will help you to understand “Java Program Of Prime Numbers.”

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number.

###### Java Program Of Prime Numbers

0 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 32 33 |
package com.codenuclear; import java.util.Scanner; public class PrintPrimeNumbers { public static void main(String args[]) { int num = 0; System.out.println("Please Enter n."); Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); scanner.close(); System.out.println("\nPrime Numbers up to " + n + "."); for (int i = 1; i <= n; i++) { int counter = 0; for (num = i; num >= 1; num--) { if (i % num == 0) { counter++; } } // Counter = 2 means number is only divisible by 1 and the number itself. So it's a prime number. if (counter == 2) { System.out.print(i + " "); } } } } |

Output