Program Input: import java.util.*; public class PrimeNumber { public static void main(String[] args) { Scanner scann...
Program Input:
import java.util.*; public class PrimeNumber { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter a number: "); int num = scanner.nextInt(); if ( num < 2 ) { System.out.println("\n There are no Prime Numbers available"); System.exit(0); } System.out.printf("\n The Prime Numbers from 1 to %d are: \n 2", num); PrimeNumber primeNum = new PrimeNumber(); for (int i = 3; i <= num; i++) { if ( primeNum.isPrime(i) ) { System.out.print(", " + i); } } } // Method to check whether the number is prime or not public boolean isPrime(int num) { if ( num < 2 ) return false; for (int i = 2; i <= Math.sqrt(num); i++) { if ( num % i == 0 ) { return false; } } return true; } }
Program Output:
Please enter a number:100 The Prime Numbers from 1 to 100 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