0

write logic to print prime numbers from up to n

please write logic for prime numbers from up to n

7th Dec 2016, 5:51 PM
Boda Ravi
2 Réponses
+ 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).
25th Dec 2016, 12:27 AM
Michael Isac Girardi
Michael Isac Girardi - avatar
10th Dec 2016, 3:51 AM
Tom Bertino, Jr
Tom Bertino, Jr - avatar