Skip to content

Instantly share code, notes, and snippets.

@richo
Created October 22, 2012 01:10
Show Gist options
  • Save richo/3929157 to your computer and use it in GitHub Desktop.
Save richo/3929157 to your computer and use it in GitHub Desktop.
/*
* prime.c
*
* Created on: Oct 21, 2012
* Author: emma
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define abs(x) ((x < 0) ? x *= -1 : x)
int isPrime(int x);
int main(int argc, char **argv) {
unsigned int temp;
int x = atoi(argv[1]);
x = abs(x);
temp = isPrime(x);
printf("Is %d prime: %d", x, temp);
return 0;
}
int isPrime(int x) {
if(x < 2)
return 0;
int temp = 1;
int i;
for(i = 2; i < x; i++) {
if(x % i == 0)
break;
else temp = 0;
}
return temp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment