C Program to Check Whether a Number is Prime or Not

 

#include<stdio.h>
#include<conio.h>
void main()
{
int n, k, rem, flag=0;
clrscr();
printf("Enter a positive number: ");
scanf("%d",&n);
for(k=2;k<=n/2 && flag==0; k++)
{
rem=n%k;
if(rem==0)
{
flag=1;
}
if(flag==0)
printf("It is a prime number %d",n);
else
printf("It is not a prime number %d",n);
getch();
}
}

 

Output:

C Program to Check Whether a Number is Prime or Not