# Prime number in C

In this program, you will write prime number program in c and check whether a number is prime or not in the c program using for loop, while loop, do-while loop.

#### what is prime number?

The number which will divide by 1 or itself is called a prime number. This means prime numbers in c have only two factors 1 and itself.

condition for prime number:

the number should be greater than 1 and have two factors.

## Prime number program in c using for loop

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

## output

```enter the number:2
2 is a prime number```

## Prime number program in c using while loop

``````#include<stdio.h>
int main()
{
int num,i,flag=0;
printf("enter the number:");
scanf("%d",&num);
i=2;
while(i<=(num/2))
{
if(num%i==0)
{
flag=1;
}
i=i+1;
}
if(flag==0)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
return 0;
}``````

## output

```enter the number:5
5 is a prime number```

## Prime number program in c using do while loop

``````#include<stdio.h>
int main()
{
int num,i,flag=0;
printf("enter the number:");
scanf("%d",&num);
i=2;
do
{
if(num%i==0)
{
flag=1;
}
i=i+1;
} while(i<=(num/2));
if(flag==0)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
return 0;
}``````

## output

```enter the number:17
17 is a prime number```