December SPECIAL! For a limited time only. Get 20% discount on a LIFETIME PRO account!Want more features on Pastebin? Sign Up, it's FREE!
tweet
Guest

Untitled

By: a guest on Sep 18th, 2015  |  syntax: C  |  size: 1.08 KB  |  views: 70  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print  |  QR code  |  clone
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2.  
  3. int main(int argc, char *argv[])
  4. {
  5.     // I'm not sure wether this array is actualy initalized if you
  6.     // write it like that
  7.     int primes[50];
  8.     int num = 0;
  9.  
  10.     // Declare iteration variables like `i` inside the for-clause
  11.     for(int i = 1; i < 100; i++) {
  12.         // Move the definition of prime in here if that's the only place you'll
  13.         // ever use it
  14.         //
  15.         // I used prime instead of not_prime, so the if-statement after the loop
  16.         // is more readable
  17.         int prime = 1;
  18.  
  19.         // Arrays in C start at index 0
  20.         for(int x = 0; x < num; x++) {
  21.             if(i % primes[x] == 0) {
  22.                 prime = 0;
  23.                 // You don't need to test with the rest of the primes
  24.                 break;
  25.             }
  26.         }
  27.  
  28.         // A non-zero integer is treated as true in C
  29.         if(prime) {
  30.             primes[num] = i;
  31.             num++;
  32.         }
  33.  
  34.     }
  35.  
  36.     // Arrays in C start at index 0
  37.     for(int i = 0; i < num; i++) {
  38.         printf("%d ",primes[i]);
  39.     }
  40.  
  41.     return 0;
  42. }
clone this paste RAW Paste Data
Top