Code to check whether a number is prime or not

 

Hey guys once again welcome to the Decoders blog, I hope you all are doing well and today I'm present with another logic but this time with something different it is a C logic which will help us to check whether a number is prime or not using C.

#include<stdio.h>
int main()
{
    int n;
    int flag=0,i=2;
    printf("Enter the number: ");
    scanf("%d",&n);
    if((n==0)||(n==1))
    {
        flag=1;
    }
    else if(n==2)
    {
        flag=0;
    }
    else
    {
        for(i=2;i<n;i++)
        {
            if(n%i==0)
            {
                flag=1;
                break;
            }
        }
    }
    if(flag==0)
    {
        printf("The number is a prime number");
    }
    else
        printf("The number is not a prime number");
    return(0);
}

Comments

Popular Posts