Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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 (count
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote