0
write logic to print prime numbers from up to n
please write logic for prime numbers from up to n
2 Respuestas
+ 1
the logic to identify a prime number is simple: it is divisible only by 1 and itself.
If you need a function to test this condition take a look at this code:
bool isPrime(int x) {
if (x<=0) return false; // i don't test these values
if (x<2) return true; // 1 and 2 are primes!
for (int i=2;i<x;i++) {
if (x%i==0) return false;
}
return true;
}
As you can see I loop from 2 to x-1 to test if x is divisible by other numbers. If not is a prime number (last return).