Question
Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name:
Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500
Method Name: isPrime(int num) and returns a boolean
Use for loop to capture all the prime numbers (1 through 500)
Create a file to add the list of prime numbers
Working Files:
public class IsPrimeMethod { public static void main(String[] args) { String input; // To hold keyboard input String message; // Message to display int number; // Number to check for prime // Get the number. // Determine whether it is prime or not. if (isPrime(number)) // print the message the number is prime //enter code here else // print the message the number is not a prime //enter code here // Display a message. JOptionPane.showMessageDialog(null, message); System.exit(0); } /** The isPrime method determines whether a number is prime. @param num The number to check. @return true if the number is prime, false otherwise. */ //create a method called isPrime() that takes and integer variable and returns a boolean public static boolean isPrime(int num) { boolean ___________________ = false; int ___________ = 2; while(_________ < num && !_______________) { if ((_______ % div) == 0) _______________ = true; ____________++; } return _________________; } }
import java.util.Scanner; import java.io.*;
/** This program demonstrates a solution to the Prime Number List programming challenge. */
public class PrimeNumberList { public static void main(String[] args) throws IOException { int number; // Number to check for prime // Open a file. PrintWriter outFile = ___________________("PrimeList.txt"); // Write the prime number list. for (int i = 1; i <= 100; i++) { if (______________)) outFile.println(________); } // Close the file. outFile.close(); } /** The isPrime method determines whether a number is prime. @param num The number to check. @return true if the number is prime, false otherwise. */ public static boolean isPrime(int num) { //Create the method created in problem 1 } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started