Implement a method named isPrime , which will take an integer as an argument and
ID: 3668285 • Letter: I
Question
Implement a method named isPrime, which will take an integer as an argument and returns true (a Boolean value) if the argument is a prime number, or false otherwise
Implement another method that will save a list of prime numbers to a text file for the number range between 1 and 9999
isPrime() must be utilized
only store the prime numbers to the file, NOT all 10,000 numbers
Include your name as an output statement.
Implement yet another method which will process an input (text) file, to be provided by your instructor, and display all of the prime numbers from it
isPrime() must be utilized
display to the console only the prime numbers from the file
ensure that you can process the prime-test-cases.txt file (my test file will be named as-is.)
Implement a main() to demonstrate your working project
Explanation / Answer
import java.util.*;
public class PrimeNumberExample {
public static void main(String args[]) {
//get input till which prime number to be printed
System.out.println("Enter the number till which prime number to be printed: ");
int limit = new Scanner(System.in).nextInt();
//printing primer numbers till the limit ( 1 to 9999)
System.out.println("Printing prime number from 1 to " + limit);
for(int number = 2; number<=limit; number++){
//print prime numbers only
if(isPrime(number)){
System.out.println(number);
}
}
}
/*
* Prime number is not divisible by any number other than 1 and itself
* @return true if number is prime
*/
public static boolean isPrime(int number){
for(int i=2; i<number; i++){
if(number%i == 0){
return false; //number is divisible so its not prime
}
}
return true; //number is prime now
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.