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: 3879012 • 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

java NthPrime 5 should print the number 11::

package com.prasad.java;

public class Example1 {

public static void main(String[] args) {

int i = 0;

int j = 0;

String primeNumbers = "";

for (i = 1; i <= 11; i++) {

int counter = 0;

for (j = i; j >= 1; j--) {

if (i % j== 0) {

counter = counter + 1;

}

}

if (counter == 2) {

primeNumbers = primeNumbers + i + ",";

}

}

System.out.println("Prime numbers from 1 to 11 are :");

System.out.println(primeNumbers);

}

}

Output::

Prime numbers from 1 to 11 are :
2,3,5,7,11,

java NthPrime should print the first 100 prime numbers separated by commas::

----------------------------------------------------------------------------------------------------------

package com.prasad.java;

public class Example1 {

public static void main(String[] args) {

int i = 0;

int j = 0;

String primeNumbers = "";

for (i = 1; i <= 100; i++) {

int counter = 0;

for (j = i; j >= 1; j--) {

if (i % j== 0) {

counter = counter + 1;

}

}

if (counter == 2) {

primeNumbers = primeNumbers + i + ",";

}

}

System.out.println("Prime numbers from 1 to 100 are :");

System.out.println(primeNumbers);

}

}

output::

Prime numbers from 1 to 100 are :
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,

java NthPrime 123 should instruct the user how to run the program::

------------------------------------------------------------------------------------------

package com.prasad.java;

public class Example1 {

public static void main(String[] args) {

int i = 0;

int j = 0;

String primeNumbers = "";

for (i = 1; i <= 123; i++) {

int counter = 0;

for (j = i; j >= 1; j--) {

if (i % j== 0) {

counter = counter + 1;

}

}

if (counter == 2) {

primeNumbers = primeNumbers + i + ",";

}

}

System.out.println("Prime numbers from 1 to 123 are :");

System.out.println(primeNumbers);

}

}

output::

-------------

Prime numbers from 1 to 123 are :
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,

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