## C Program To Check Prime Number

Learn How To Check Prime Number in C Programming Language. It is important that we should know** How A For Loop Works **before getting further with this C Program.

#### What is a Prime Number?

A Prime Number is a **Natural Number Greater than 1** and it should have **No Positive Divisors other than 1** and the **Number itself**. A Natural Number greater than 1 but not a Prime Integer is known as a Composite Number.

#### Example

**2, 3, 5, 7 ,11**

**These numbers are evenly divided by 1 and the number itself.**

**Must Read: C Program To Find Prime Numbers using Sieve of Eratosthenes Algorithm**

#### Method 1: C Program To Find Prime Number using For Loop

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <stdio.h> int main() { int num, count, temp = 0; printf("\nEnter a Number:\t"); scanf("%d", &num); for(count = 2; count <= num/2; count++) { if(num%count == 0) { temp = 1; break; } } if(temp == 0) { printf("\n%d is a Prime Number\n", num); } else { printf("\n%d is Not a Prime Number\n", num); } return 0; } |

#### Method 2: C Program To Check Prime Numbers using While Loop

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <stdio.h> int main() { int num, count = 2, temp = 0; printf("\nEnter a Number:\t"); scanf("%d", &num); while(count <= num - 1) { if(num%count == 0) { temp = 1; break; } count++; } if(temp == 0) { printf("\n%d is a Prime Number\n", num); } else { printf("\n%d is Not a Prime Number\n", num); } return 0; } |

**Also Read: Convert Number To Words in C Programming**

#### Output

Alternatively, you can also use this Condition in the Loop.

**for(count=2;count<=num/2;count++)**

The above Condition **count<=num/2** is an efficient Algorithm to Check Prime Integers and helps to reduce the Number of Comparisons and thereby efficient time complexity.

#### Method 3: Sieve Method To Find Prime Numbers in C Programming

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include <stdio.h> #include <math.h> void sieve_method(int limit, int arr[]) { int m, n; for(m = 0; m < limit; m++) { arr[m] = 1; } arr[0] = 0, arr[1] = 0; for(m = 2; m < sqrt(limit); m++) { for (n = m*m; n < limit; n = n + m) { arr[n] = 0; } } } int main() { int count, limit; printf("\nEnter The Limit:\t"); scanf("%d", &limit); int arr[limit]; sieve_method(limit, arr); printf("\n"); for (count = 0; count < limit; count++) { if (arr[count] == 1) { printf("%d\t", count); } } printf("\n"); return 0; } |

If you have any compilation errors or doubts in this C Program To Check Prime Numbers, let us know about in the Comment Section below.

What is this sieve method for finding prime numbers about? Its quite confusing.

This is really good. So many methods to print Prime Numbers. Thanks.

I used to get lost in the For loop conditions. This is by far the best code for Prime Number checking that I have come across so far.

Amazing. I had never heard that there something called as Sieve method to find Prime Numbers in C programming. Thanks. You introduce so mamy concepts that no has even heard of.

while(count <= num – 1)