0
How to find prime numbers using functions
input 5..... then output should be 5 prime numbers
1 Resposta
+ 3
int times = 5;
int[ ] primes = new int [times];
int primeIndex = 0;
int primesFound = 0;
boolean isPrime = true;
for (int i = 2; ; i++) {
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primes [primeIndex++] = i;
primesFound++;
} else {
isPrime = true;
}
if (primesFound == times) {
break;
}
}