Program 1: Program Name ID: ID001 Make sure you include this ID as a System.out.
ID: 3729219 • Letter: P
Question
Program 1: Program Name
ID: ID001
Make sure you include this ID as a System.out.println() message at the beginning of your program. This helps us with grading, and is required.
Description:
Part 1
You will write a program that will determine whether a number is a prime number. Allow the user to enter a number, and display a message depending on whether or not it is prime. The user should be allowed to keep entering numbers until they enter “-1” to quit.
Part 2
As a second part to this program, allow the user to enter a max number. Then, display each number from 1 to the user-inputted range, as well as whether or not each number is prime.
For example, let’s say the user enters 10 as the max range. The program would show:
1 is prime.
2 is prime.
3 is prime.
4 is not prime.
5 is prime.
6 is not prime.
7 is prime.
8 is not prime.
9 is not prime.
10 is not prime.
Explanation / Answer
Part#1:
CheckPrimeOrNot.java
import java.util.Scanner;
public class CheckPrimeOrNot {
public static void main(String[] args) {
// Declaring variables
int num;
final int SENTINEL = -1;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
/* This while loop continues to execute
* until the user enters -1
*/
do {
System.out.print(" Enter a number :");
num = sc.nextInt();
// Getting the input entered by the user
if (num != SENTINEL) {
if (isPrime(num))
System.out.print(num + " is Prime.");
else
System.out.print(num + " is not Prime.");
}
} while (num != SENTINEL);
}
//This method will check whether the number is prime or not
public static boolean isPrime(int number) {
// If the user entered number is '2' return true
if (number == 1)
return false;
for (int i = 2; i * i <= number; i++) {
if (number % i == 0)
return false;
}
return true;
}
}
_____________________
Output:
Enter a number :45
45 is not Prime.
Enter a number :67
67 is Prime.
Enter a number :78
78 is not Prime.
Enter a number :89
89 is Prime.
Enter a number :91
91 is not Prime.
Enter a number :-1
__________________
Part#2:
PrimeOrNot.java
import java.util.Scanner;
public class PrimeOrNot {
public static void main(String[] args) {
// Declaring variables
int num;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
// Getting the input entered by the user
System.out.print(" Enter a number :");
num = sc.nextInt();
for (int i = 1; i <= num; i++) {
if (isPrime(i))
System.out.println(i + " is Prime.");
else
System.out.println(i + " is not Prime.");
}
}
// This method will check whether the number is prime or not
public static boolean isPrime(int number) {
// If the user entered number is '2' return true
if (number == 1)
return true;
for (int i = 2; i * i <= number; i++) {
if (number % i == 0)
return false;
}
return true;
}
}
______________________
Output:
Enter a number :10
1 is Prime.
2 is Prime.
3 is Prime.
4 is not Prime.
5 is Prime.
6 is not Prime.
7 is Prime.
8 is not Prime.
9 is not Prime.
10 is not Prime.
________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.