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

Determine the set of prime numbers between 2 -100. There are 25 of these. Your p

ID: 3642020 • Letter: D

Question

Determine the set of prime numbers between 2 -100. There are 25 of these. Your program should use a single method, described below, to determine if a number is prime or not. Once the program determines that a number is prime, it will print that result to an output file.

To determine if a given number is prime or not, simply divide it by all of the numbers greater than or equal to 2 that are less than itself. If any of those smaller numbers evenly divide the number, i.e., with no remainder, then the number is not prime. If none of them evenly divide the number, then the number is prime. (Note that there are other, more efficient ways of determining whether a number is prime or not, but this will suffice for now.)

Your program should have a method that determines whether a given number is prime or not. The header will be:

public static boolean isPrime (int numToCheck)

It will take a single integer value as input, determine whether it is prime or not using the algorithm described above, and then return that result as a boolean value.

Once you have a method that determines primality, loop through the numbers between 2 and 100 and determine if they are prime or not by using that method. Make a table of your findings as described below. Your results should be printed in an output file called PrimesOut.txt. In addition, your program should print a final summary of all 25 primes between 2 and 100 on a single line in the output file.

The output to the file PrimesOut.txt should look like:

Number Prime?
------ ------
2 yes
3 yes
4 no
5 yes
6 no
. . . . . .

List of Primes: 2,3,5,7,11,13,

Explanation / Answer

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class PrimeNumbers {
  
    public static boolean isPrime(int numToCheck) {
        if (numToCheck == 2) {
            return true;
        }
        else if (numToCheck % 2 == 0) {
            return false;
        }
        else {
            for (int i = 3; i <= (int)Math.sqrt(numToCheck); i += 2) {
                if (numToCheck % i == 0) {
                    return false;
                }
            }
        }
        return true;
    }
  
    public static void main(String[] args) throws IOException {
        PrintWriter fileOutput = new PrintWriter(
                new FileWriter("PrimesOut.txt"));
        String primeList = "List of Primes: ";
      
        fileOutput.println("Number   Prime?");
        fileOutput.println("------   ------");
        for (int i = 2; i <= 100; i++) {
            fileOutput.printf("%4d", i);
            if (isPrime(i)) {
                fileOutput.println(String.format("%9s ", "yes"));
                primeList += i + ",";
            }
            else {
                fileOutput.println(String.format("%9s ", "no"));
            }
        }
        primeList = primeList.substring(0, primeList.length() - 1); //Eliminate the last comma
        fileOutput.println();
        fileOutput.println(primeList);
      
        fileOutput.close();
        System.out.println("Done");
    }
}

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