In this tutorial, you will learn how to write a **C program to check whether a number is prime or not.**

A positive number is called prime number if it is **divisible by 1 and itself**. For example: 13, 19, 23 are prime numbers because they are divisible by 1 and themselves.

## Program to check whether a number is prime or not

In this example, we are checking whether the number entered by user is a prime number or not.

The first check we are doing is by comparing the input number with 0 and 1, **if the number is either 0 or 1, it is not a prime number.**

The second check we are doing here is checking whether the input number is divisible by any number between 2 to num/2, if it is then its not a prime number else it is a prime number.

```
#include <stdio.h>
int main() {
//Here flag value 0 means prime number and
//1 means non-prime number
int num, i, flag = 0;
printf("Enter a number to check whether prime or not: ");
scanf("%d", &num);
// 0 and 1 are not the prime numbers so setting
// the flag to 1 if entered number is 0 or 1
if (num == 0 || num == 1)
flag = 1;
for (i = 2; i <= num / 2; ++i) {
// if num is divisible by i, then num is not prime
// change the flag value to 1
if (num % i == 0) {
flag = 1;
break;
}
}
// If flag is 0 it means the number is prime
if (flag == 0)
printf("Entered number %d is a prime number.", num);
else
printf("Entered number %d is not a prime number.", num);
return 0;
}
```

**Output:** User enters the number 13, which is a prime number so the program prints the message that “Entered number 13 is a prime number”.