Program to Check Whether a Number is Prime or Not - C Program

Program to Check Whether a Number is Prime or Not

A Prime Number can be divided evenly only by 1 or itself. And it must be a whole number greater than 1. Example: 5 can only be divided evenly by 1 or 5, so it is prime number

#include <stdio.h>
int main()
{
    int num, d;
    int isPrime = 1;
    printf("Enter the Number :");
    scanf("%d",&num);
    if(num==1)
        printf("Not a prime number");
    else
    {
        for (d = 2; d <= num/d; d++)
        {
            if (num % d == 0)
            {
                isPrime = 0;
                break;
            }
        }
        if(isPrime==1) 
            printf("Number is a Prime Number");
        else
            printf("Not a prime number");
    }
            
    return 0;
}

Output - Program to Check Whether a Number is Prime or Not

Enter the Number :17                                 
Number is a Prime Number   
Enter the Number :18 
Not a prime number    
Compile and Run Online