Advertisement
Vladkoheca

PrimeNums.java

Oct 26th, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.66 KB | Source Code | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PrimeNums {
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.  
  7.         System.out.print("Enter a number: ");
  8.         int n = sc.nextInt();
  9.  
  10.         System.out.println("Prime numbers up to " + n + " are: ");
  11.         for (int i = 2; i <= n; i++) {
  12.             boolean isPrime = true;
  13.             for (int j = 2; j <= Math.sqrt(i); j++) {
  14.                 if (i % j == 0) {
  15.                     isPrime = false;
  16.                     break;
  17.                 }
  18.             }
  19.             if (isPrime) {
  20.                 System.out.println(i + " ");
  21.             }
  22.         }
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement