
Untitled
By: a guest on
Sep 18th, 2015 | syntax:
C | size: 1.08 KB | views:
70 | expires: Never
#include <stdio.h>
int main(int argc, char *argv[])
{
// I'm not sure wether this array is actualy initalized if you
// write it like that
int primes[50];
int num = 0;
// Declare iteration variables like `i` inside the for-clause
for(int i = 1; i < 100; i++) {
// Move the definition of prime in here if that's the only place you'll
// ever use it
//
// I used prime instead of not_prime, so the if-statement after the loop
// is more readable
int prime = 1;
// Arrays in C start at index 0
for(int x = 0; x < num; x++) {
if(i % primes[x] == 0) {
prime = 0;
// You don't need to test with the rest of the primes
break;
}
}
// A non-zero integer is treated as true in C
if(prime) {
primes[num] = i;
num++;
}
}
// Arrays in C start at index 0
for(int i = 0; i < num; i++) {
printf("%d ",primes[i]);
}
return 0;
}