C Program to Check Whether a Given Number is Prime Number or Not
A Prime Number is a positive integer greater than 1 which is divisible by 1 and itself. In other words, Prime number is a whole number greater than whose factors are 1 and itself. Examples of first few prime numbers are 2, 3, 5, 7, 11, 13..
Note : Number 2 is first and only even prime number.
Program
#include<stdio.h>
#include<conio.h>
int main()
{
int number, i, flag=0;
clrscr();
// Inputs
printf("Enter integer number: ");
scanf("%d", &number);
// Checking for prime
for(i=2;i<=number/2; i++)
{
if(number%i==0)
{
flag = 1;
break;
}
}
// Taking decision
if(flag==0 && number>=2)
{
printf("%d is PRIME.", number);
}
else
{
printf("%d is NOT PRIME.", number);
}
getch();
return(0);
}
Output of above program :
Run 1: ----------- Enter integer number: 13 ↲ 13 is PRIME. Run 2: ----------- Enter integer number: 16 ↲ 16 is NOT PRIME. Run 3: ----------- Enter integer number: 1 ↲ 1 is NOT PRIME. Run 4: ----------- Enter integer number: 2 ↲ 2 is PRIME. Note: ↲ indicates enter is pressed.