I am an amateur java student, and my teacher gave me this assignment which is to write a code that list the first 100 prime numbers. Here is what I have I tried so far.
for(int i =1; true; i++) {
int k = 0;
for(int j = 1; j <= i; j++) {
if(i % j == 0){
k ++;
}
if(k == 2) {
System.out.println(i);
}
}
}
The problem I encountered is I couldn't not stop the console to stop after a count of 100, even if I used possible mechanisms I am familiar with. for instance:
for( int i =1; true; i++) {
int k = 0;
for(int j = 1; j <= i; j++) {
if(i % j == 0) {
k ++;
}
int m = 0;
if(k == 2) {
System.out.println(i);
m++;
if(m==100) {
break;
}
}
}
}
Can I get help in terminating the loop after a count of 100 primes ?