Question:
Part I:
An integer is prime if it is only divisible by 1 and itself. For example, 7 is prime, but 9 is not (since it is divisible by 3). Write a method that determines if a given integer is prime. Call it isPrime. It should return a boolean value.
Note: The Java % operator is useful for determining divisibility.
Part II:
Create a menu-driven program.
Let the user choose whether they want to display a list of prime numbers or non-prime numbers. They should enter 1 for prime, 2 for non-prime and 3 to Quit.
Then ask the user how many numbers they want to display. Store this in numValues.
If the user chose to display prime numbers, display the first numValues prime numbers as a comma separated list. (For example, if the user enters 5, display "2, 3, 5, 7, 11".)
If the user chose to display non-prime numbers, display the first numValues non-prime numbers as a comma separated list. (For example, if the user enters 5, display "4, 6, 8, 9, 10".)
You should use a loop that calls the isPrime method.
An integer is prime if it is only divisible by 1 and itself. For example, 7 is prime, but 9 is not (since it is divisible by 3). Write a method that determines if a given integer is prime. Call it isPrime. It should return a boolean value.
Note: The Java % operator is useful for determining divisibility.
Part II:
Create a menu-driven program.
Let the user choose whether they want to display a list of prime numbers or non-prime numbers. They should enter 1 for prime, 2 for non-prime and 3 to Quit.
Then ask the user how many numbers they want to display. Store this in numValues.
If the user chose to display prime numbers, display the first numValues prime numbers as a comma separated list. (For example, if the user enters 5, display "2, 3, 5, 7, 11".)
If the user chose to display non-prime numbers, display the first numValues non-prime numbers as a comma separated list. (For example, if the user enters 5, display "4, 6, 8, 9, 10".)
You should use a loop that calls the isPrime method.
Code:
import java.util.Scanner; public class PrimeNumber { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int choice=0; int numValues=0; while(choice != 3){ System.out.println("1 : Prime "); System.out.println("2 : Non Prime "); System.out.println("3 : Quit"); System.out.print("Enter choice: "); choice= sc.nextInt(); if(choice != 3){ System.out.print("Enter count: "); numValues = sc.nextInt(); if(choice==1){ for(int i=2,count=1;count<=numValues;i++){ if(isPrime(i)){ System.out.print(i); if(count<numValues){ System.out.print(", "); } count=count +1; } } } if(choice==2){ for(int i=2,count=1;count<=numValues;i++){ if(! isPrime(i)){ System.out.print(i); if(count<numValues){ System.out.print(", "); } count=count +1; } } } System.out.println(); } } } public static boolean isPrime(int x) { for(int i=2; i<x; i++) { if(x % i == 0) { return false; } } return true; } }
Output:
$ java PrimeNumber 1 : Prime 2 : Non Prime 3 : Quit Enter choice: 1 Enter count: 5 2, 3, 5, 7, 11 1 : Prime 2 : Non Prime 3 : Quit Enter choice: 2 Enter count: 5 4, 6, 8, 9, 10 1 : Prime 2 : Non Prime 3 : Quit Enter choice: 3