211
Computers and Technology / Re: C code.
« on: April 08, 2017, 08:26:47 pm »Use this function to check if a number is prime or not:
int is_prime(int num)
{
if (num <= 1) return 0;
if (num % 2 == 0 && num > 2) return 0;
for(int i = 3; i < num / 2; i+= 2)
{
if (num % i == 0)
return 0;
}
return 1;
}
If you looking for faster code use sqrt(num) instead of num/2 in the loop.
Forgot to mention however in this assignment I am not allowed to use anything other than int main ()
:/
it would've been super easy with functions tho ;P
Thanks, adam.
Well you can use the loop in this function in the main()