0

How to find prime numbers using functions

input 5..... then output should be 5 prime numbers

8th Apr 2017, 1:02 AM
Phani Datta Reddy
Phani Datta Reddy - avatar
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; } }
8th Apr 2017, 1:26 AM
Edward