From 866bd5756fdf111b692fd2cbae4bef92d3751315 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20L=C3=B8vbr=C3=B8tte=20Olsen?= Date: Thu, 27 Aug 2015 21:19:24 +0200 Subject: [PATCH] Added isPrime.c Checks if a number (n) is a prime, also lists all it's factors --- isPrime.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 isPrime.c diff --git a/isPrime.c b/isPrime.c new file mode 100644 index 0000000..83ce81a --- /dev/null +++ b/isPrime.c @@ -0,0 +1,31 @@ +#include +#include + +int main(int argc, char* argv[]) +{ + if (argc != 2) + { + printf("Usage = ./isPrime "); + return -1; + } + + int n = atoi(argv[1]); + int results = 0; + + for (int i = 1; i < n + 1; i++) + { + if (n % i == 0) + { + printf("%i is divisible by %i\n", n, i); + results++; + } + } + if (results == 2) + { + printf("%i is a prime.\n", n); + } + else + { + printf("%i is not a prime, as it's divisible by %i numbers other than 1 and %i\n", n, results - 2, n); + } +}