isPrime (C)

isPrime.c

#include <stdio.h>
#include <math.h>

int isPrime(const int);

int main(void)
{
        int i;
        for (i = -1 ; i <= 15 ; ++i)
            printf("%d is %sprime\n", i, isPrime(i) ? "" : "NOT ");
        return 0;
}

int isPrime(const int number)
{
    if(number <= 1) 
       return 0;
    int i, r, prime = 1;
    if(number != 2 && number % 2 == 0)
       prime = 0;
    else
        for(i = 3, r = sqrt(number) ; i <= r && prime ; i += 2)
            if (number % i == 0)
                prime = 0;
    return prime; 
}

This code was developed by me, G. Samaras.

Have questions about this code? Comments? Did you find a bug? Let me know!😀
Page created by G. (George) Samaras (DIT)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s