# C Program to Check Whether a Number is Prime or not

Given a positive integer . The task is to write a C program to check if the number is **prime or not**.

**Definition**:

A

prime numberis a natural number greater than 1 that has no positive divisors other than 1 and itself. The first few prime numbers are {2, 3, 5, 7, 11, ….}

The idea to solve this problem is to iterate through all the numbers starting from 2 to (N/2) using a for loop and for every number check if it divides N. If we find any number that divides, we return false. If we did not find any number between 2 and N/2 which divides N then it means that N is prime and we will return True.

Below is the C program to check if a number is prime:

`// C program to check if a ` `// number is prime ` ` ` `#include <stdio.h> ` ` ` `int` `main() ` `{ ` ` ` `int` `n, i, flag = 1; ` ` ` ` ` `// Ask user for input ` ` ` `printf` `(` `"Enter a number: \n"` `); ` ` ` ` ` `// Store input number in a variable ` ` ` `scanf` `(` `"%d"` `, &n); ` ` ` ` ` `// Iterate from 2 to n/2 ` ` ` `for` `(i = 2; i <= ` `sqrt` `(n) / 2; i++) { ` ` ` ` ` `// If n is divisible by any number between ` ` ` `// 2 and n/2, it is not prime ` ` ` `if` `(n % i == 0) { ` ` ` `flag = 0; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `if` `(flag == 1) { ` ` ` `printf` `(` `"%d is a prime number"` `, n); ` ` ` `} ` ` ` `else` `{ ` ` ` `printf` `(` `"%d is not a prime number"` `, n); ` ` ` `} ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output**:

Enter a number: 11 11 is a prime number

## Recommended Posts:

- C++ Program to check Prime Number
- Python program to check whether a number is Prime or not
- Check if a prime number can be expressed as sum of two Prime Numbers
- PHP | Check if a number is prime
- Check if a number is a Pythagorean Prime or not
- Check if a number is Quartan Prime or not
- Check whether a number is circular prime or not
- Check whether N is a Dihedral Prime Number or not
- Check a number for Permutable Prime
- Check whether the given number is Wagstaff prime or not
- Check if a number is Primorial Prime or not
- Check if the first and last digit of number N is prime and their sum is less than K
- Check if a number is Full Prime
- Check if a number can be written as a sum of 'k' prime numbers
- Check if the first and last digit of the smallest number forms a prime

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.