import java.util.Scanner;
//Start of class PrimeNumbers
public class PrimeNumbers {
/** Static boolean method that checks if an integer number is prime
* or not and returns true or false correspondingly
*/
static boolean checkNumber(int number){
//for loop for checking integer numbers
for(int i=2;i<number;i++){
//if statement returning false if a number
//has not a remainder after division with number 2
if(number%i==0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int num;
Scanner scanNumber= new Scanner(System.in);
System.out.print("Please enter a number: ");
//Reads an integer value
num=scanNumber.nextInt();
//if-else statement to check if the entered number is negative
if (num<0){
//if statements executed if entered number is negative
System.err.println("Error!You inserted a negative number");
System.err.println("The program will now exit");
//Terminates the program
System.exit(0);
}
//else statements executed if the entered number is not negative
else {
//Boolean variable for storing checkNumber method's variables
// and checks if a number is prime or not
boolean prime=checkNumber(num);
//if checkNumber method returns true
if (prime==true){
System.out.println("Is prime");
}
//if checkNumber method returns false
else{
System.out.println("Is not prime");
}
System.out.println("Prime numbers <= entered number are:");
//for loop for checking all entered numbers
for(int i = 2; i <= num; i++ ){
//prime variable set to true
prime = true;
//for loop for checking and printing prime
//numbers between 2 and the entered number
for(int j=2; j <= i/2; j++) {
//if statement that sets prime variable as false
//if the number is not prime
if (i%j == 0){
prime = false;
}
}
//If statement that is executed not only if a number is prime
if (prime){
//Displays numbers seperated by a comma
System.out.print(i+",");
}
}
}
}
//End of class PrimeNumbers
}