Advertisement
brainuser5705

more prime

Apr 24th, 2021
88
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None
  1. // Program to print prime numbers
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4.  
  5. int main ()
  6. {
  7.    
  8.     int p, i, primes[50], primeIndex = 2;
  9.     bool isPrime;
  10.  
  11.     primes[0] = 2;
  12.     primes[1] = 3;
  13.  
  14.     for (p = 5; p <= 50; p += 2){
  15.         isPrime = true;
  16.  
  17.         //starts at 1 because even numbers are not considered
  18.         for (i = 1; isPrime && (p / primes[i] >= primes[i]); i++) // does not exceed square roots of the other primes
  19.             if (p % primes[i] == 0) // not divisible by the primes
  20.                 isPrime = false;
  21.  
  22.         if (isPrime == true){
  23.             primes[primeIndex] = p;
  24.             primeIndex++;
  25.         }
  26.     }
  27.  
  28.     for (i = 0; i < primeIndex; i++){
  29.         printf("%i ", primes[i]);
  30.     }
  31.  
  32.     printf("\n");
  33.    
  34.  
  35.     return 0;
  36. }
  37.  
Advertisement
RAW Paste Data Copied
Advertisement