-
Notifications
You must be signed in to change notification settings - Fork 21
/
ex10.c
44 lines (35 loc) · 879 Bytes
/
ex10.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* 10. Write a function prime() that returns 1 if its argument is a prime
* number and returns 0 otherwise.
*
* By Faisal Saadatmand
*/
#include <stdio.h>
#include <stdbool.h>
/* functions */
bool prime(int);
bool prime(int number)
{
int i;
/* Exclude all numbers under 2 and all even numbers.
* 2 is a special case: though it's even, it's a prime. */
if (number != 2 && (number < 2 || number % 2 == 0))
return false;
/* check if number is divisible by any number other than itself */
for (i = 3; i < number - 1; i += 2)
if (number % i == 0)
return false;
return true;
}
int main (void)
{
int number, input;
while (true) {
printf("Enter a number to check for primality: ");
if (!(input = scanf("%i", &number)) || input == EOF)
break;
printf(" %i %s a prime number.\n", number,
(prime(number)) ? "is" : "is NOT");
}
return 0;
}