So here's my problem that I have for homework.
Write a program that takes a command-line argument N (integer, N>= 1) and prints the number of primes less than or equal to N. A number is prime if it is divisible only by itself and 1.
Here's the code that I've come up with so far, and I'm sure that there's plenty of errors to it.
import java.util.Random;
public class PrimeCounter {
public static void main(String[] args) {
Random rand = new Random();
int N = rand.nextInt();
for(int number = 2; 1 <= N; number++){
if(isPrime(number)){
System.out.println(number);
}
}
}
public static boolean isPrime(int number){
for(int i=2; i<=number; i++){
if(number%i == 0){
return false;
}
}
return true;
}
}
Any suggestions? Every time I run it through the unit tests through eclipse I fail all of the tests my professor has given me. From the way that I understand the assignment, he wants the program to take the number N and print out how many prime numbers are less than or equal to N. The problem is that we haven't gone over inputs yet so I'm not sure how he wants us to do this program without having some kind of input line for N?
N
is a command-line argument, so it will be inargs
inmain
.for(int i=2; i<number; i++){
asnumber % number
is always == 0