Prime numbers are natural numbers divisible by only one and themselves. Write a
ID: 3744179 • Letter: P
Question
Prime numbers are natural numbers divisible by only one and themselves. Write a Java program called NthPrime that when run from the command line prints the first 100 primes separated by commas when no parameters are supplied, prints the Nth prime if any number N from 1 to 100 is provided, or prints a usage message on how to use the program if any other input is provided.
For example:
java NthPrime 5 should print the number 11
java NthPrime should print the first 100 prime numbers separated by commas
java NthPrime 123 should instruct the user how to run the program
Explanation / Answer
public class NthPrime { public static boolean isPrime(int n) { for(int i = 2; i 0) { n = Integer.parseInt(args[0]); if(n > 100 || n < 0) { System.out.println("Error. argument can not be more than 100"); System.exit(-1); } } int i = 2, count = 0; while (countRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.