Pages

Prime check Algorithm

#include <stdio.h>
#include <stdlib.h>
int isPrime(int num);
int main(int argc, char* argv[])
{
int arg = 5;
if(argc > 1)
{
arg = atoi(argv[1]);
}
printf("The argument is %d\n", arg);
if(isPrime(arg))
{
printf("The number %d is prime\n", arg);
}
else
{
printf("The number %d is not prime\n", arg);
}
}
int isPrime(int num)
{
int i=2;
int mod=0;
for ( ; i<=(int)(num/2); i++)
{
mod=num%i;
printf("The modulus is %d\n", mod);

if(mod==0)
{
printf("%d divisible by %d\n", num, i);
return 0;
}
}
return 1;
}

No comments:

Post a Comment

If you like to say anything (good/bad), Please do not hesitate...