Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Program to print prime numbers
- #include <stdio.h>
- #include <stdbool.h>
- int main ()
- {
- int p, i, primes[50], primeIndex = 2;
- bool isPrime;
- primes[0] = 2;
- primes[1] = 3;
- for (p = 5; p <= 50; p += 2){
- isPrime = true;
- //starts at 1 because even numbers are not considered
- for (i = 1; isPrime && (p / primes[i] >= primes[i]); i++) // does not exceed square roots of the other primes
- if (p % primes[i] == 0) // not divisible by the primes
- isPrime = false;
- if (isPrime == true){
- primes[primeIndex] = p;
- primeIndex++;
- }
- }
- for (i = 0; i < primeIndex; i++){
- printf("%i ", primes[i]);
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement