Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class PrimeNums {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("Enter a number: ");
- int n = sc.nextInt();
- System.out.println("Prime numbers up to " + n + " are: ");
- for (int i = 2; i <= n; i++) {
- boolean isPrime = true;
- for (int j = 2; j <= Math.sqrt(i); j++) {
- if (i % j == 0) {
- isPrime = false;
- break;
- }
- }
- if (isPrime) {
- System.out.println(i + " ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement