866bd5756f
Checks if a number (n) is a prime, also lists all it's factors
32 lines
599 B
C
32 lines
599 B
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
if (argc != 2)
|
|
{
|
|
printf("Usage = ./isPrime <n>");
|
|
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);
|
|
}
|
|
}
|