10-11-2010, 12:41 PM
I'm in the process of learning C, so to exercise my loop knowledge, I created a little function to determine if a number is prime or not. A return value of 1 indicates that it is; 0 that it isn't.
Usage:
And here's the code:
I guess beginners could learn from it. It's pretty much compatible with C++ too.
Usage:
Code:
isprime(number to assess);
And here's the code:
Code:
int isprime(int x)
{
int prime = 0;
int i = 0;
for (i = (x - 1); i > 1; --i)
{
if ((x % i) == 0)
{
prime = 0;
return prime;
}
else
{
prime = 1;
}
}
return 1;
}
I guess beginners could learn from it. It's pretty much compatible with C++ too.