A prime number is an integer that is evenly divisible only by 1 and itself. For
ID: 3827518 • Letter: A
Question
A prime number is an integer that is evenly divisible only by 1 and itself. For example, the only integers that divide 7 evenly are 1 and 7, thus 7 is a prime number. On the other hand, a number that is not prime is said to be composite. An integer n is composite if there exist integers a and b such that n = ab, with 1 < a < n, and 1 < b < n. For example, the number 24 is composite since it can be factored in the following ways: 24 = 2 X 12, 24 = 3 X 8, and 24 = 4 X 6, with other factorizations possible, including 24 = 1 X 24 (but not only this way).Write a method named isPrime, which takes an integer as an argument and returns true if the argument is prime or false otherwise. Write a driver program with a main method that calls the isPrime method to demonstrate that it works.
Finally, modify your program so that it makes use of the isPrime method to store a list of all the prime numbers from 1 to 100 in a text file.
The list should look like the following:
2
3
5
.
.
.
97 A prime number is an integer that is evenly divisible only by 1 and itself. For example, the only integers that divide 7 evenly are 1 and 7, thus 7 is a prime number. On the other hand, a number that is not prime is said to be composite. An integer n is composite if there exist integers a and b such that n = ab, with 1 < a < n, and 1 < b < n. For example, the number 24 is composite since it can be factored in the following ways: 24 = 2 X 12, 24 = 3 X 8, and 24 = 4 X 6, with other factorizations possible, including 24 = 1 X 24 (but not only this way).
Write a method named isPrime, which takes an integer as an argument and returns true if the argument is prime or false otherwise. Write a driver program with a main method that calls the isPrime method to demonstrate that it works.
Finally, modify your program so that it makes use of the isPrime method to store a list of all the prime numbers from 1 to 100 in a text file.
The list should look like the following:
2
3
5
.
.
.
97 A prime number is an integer that is evenly divisible only by 1 and itself. For example, the only integers that divide 7 evenly are 1 and 7, thus 7 is a prime number. On the other hand, a number that is not prime is said to be composite. An integer n is composite if there exist integers a and b such that n = ab, with 1 < a < n, and 1 < b < n. For example, the number 24 is composite since it can be factored in the following ways: 24 = 2 X 12, 24 = 3 X 8, and 24 = 4 X 6, with other factorizations possible, including 24 = 1 X 24 (but not only this way).
Write a method named isPrime, which takes an integer as an argument and returns true if the argument is prime or false otherwise. Write a driver program with a main method that calls the isPrime method to demonstrate that it works.
Finally, modify your program so that it makes use of the isPrime method to store a list of all the prime numbers from 1 to 100 in a text file.
The list should look like the following:
2
3
5
.
.
.
97
Explanation / Answer
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class PrimeNum {
private static final String FILENAME = "C:\Users\Desktop\java\output.txt";
public boolean isPrime(int number){
for(int i=2; i<=number/2; i++){
if(number % i == 0){
return false;
}
}
return true;
}
public static void main(String a[]){
PrimeNum mpc = new PrimeNum();
try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME))){
for (int i=2;i<100;i++){
if ((mpc.isPrime(i)) == true){
bw.write(i+" ");
bw.newLine();
}
}
}
catch (IOException e){
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.