Search This Blog

Loading...

C Program to find Number is Prime or Not ?



//How to Find Out if a Number is Prime or Not ????


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

bool isprime(int n)
{
    int i,m;

    m = (int)sqrt(n);

    if(n < 2)return false;
    if(n == 2)return true;
    if(n%2 == 0)return false;

    for(i=3;i<=m;i+=2)
        if(n%i == 0)
            return false;
}
int main()
{
    int i,n;
    while(scanf("%d",&n) == 1)
    {
        if(isprime(n))
        {
            printf("Prime\n");
        }
        else printf("Not prime\n");
    }
    return 0;
}

No comments:

Post a Comment