I am writing a code that write all the prime numbers from 2 to 1000 in a file, named primes.txt. For some reason I am not able to figure out the correct way to do this problem.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class Problem6 {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
PrintWriter prw = new PrintWriter("primes.txt");
for (int i = 2; i <= 1000; i++){
if (checkIfPrime(i) == true){
System.out.println(i);
prw.println(i);
}
}
}
public static boolean checkIfPrime (int num){
boolean isPrime = true;
for (int i = 2; i <= 1000; i++){
if ( num % i == 0 ){
isPrime = false;
}
}
return isPrime;
}
}
I just dont know what to do... Please help Thanks!