Monday 15 May 2017

C Program to Check Whether a Number is Prime or Not

C Program to Check Whether a Number is Prime or Not


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=2,f=1;
clrscr();
printf("Enter a Number other than 1");
scanf("%d",&n);
while(i*i<=n)
{
if(n%i==0)
{
f=0;
break;
}
i++;

}
if(f)
printf("Prime Number");
else
printf("Not Prime Number");
getch();
}

To check whether a number is prime or not we need to check that if the number is divisible by any of the numbers lying between 2 to squareroot(n). If it is divisible by any one of these then it is not prime otherwise the number is prime.