Last active
December 14, 2015 05:59
-
-
Save mrmikejones/5039635 to your computer and use it in GitHub Desktop.
Check if a number is a prime number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
bool IsPrime(int number) | |
{ | |
if(number == 2) return true; | |
if(number % 2 == 0 || number == 1) return false; | |
bool isPrime = true; | |
int n = 3; | |
while(isPrime && n*n <= number) | |
{ | |
if(number%n == 0) | |
{ | |
isPrime = false; | |
break; | |
} | |
else | |
{ | |
n += 2; | |
} | |
} | |
return isPrime; | |
} | |
int main(int argc, char* argv[]) | |
{ | |
const int NUMBER1 = 9; | |
const int NUMBER2 = 24; | |
const int NUMBER3 = 13; | |
printf("%i %s a prime number\n", NUMBER1, IsPrime(NUMBER1) ? "is" : "is not"); | |
printf("%i %s a prime number\n", NUMBER2, IsPrime(NUMBER2) ? "is" : "is not"); | |
printf("%i %s a prime number\n", NUMBER3, IsPrime(NUMBER3) ? "is" : "is not"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment