In this tutorial, i am going to show you how to check whether a number is prime or not with the help of for loop, while loop, and function.

A number that’s only divisible by 1 and itself is named a Prime Number. For Example, 3, 5, 7, 11 are Prime Numbers.

## Programs to Check Whether a Number is Prime or Not in C

- Algorithm to Check a Number is Prime or Not
- C Program to Check Prime Number Using For Loop
- C Program to Check Prime Number Using While Loop
- C Program to Check Prime Number Using Function

### Algorithm to Check a Number is Prime or Not

Follow the below given algorithm to write a program to check a number is prime or not; as follows:

Step 1:StartStep 2:Read numbernStep 3:Set f=0Step 4:For i=2 to n-1Step 5:If n mod 1=0 thenStep 6:Set f=1 and breakStep 7:LoopStep 8:If f=0 then print 'The given number is prime' else print 'The given number is not prime'Step 9:Stop

### C Program to Check Prime Number Using For Loop

#include <stdio.h> int main() { int i, Number, count = 0; printf("\n Please Enter any number to Check for Prime :- "); scanf("%d", &Number); for (i = 2; i <= Number/2; i++) { if(Number%i == 0) { count++; break; } } if(count == 0 && Number != 1 ) { printf("\n %d is a Prime Number", Number); } else { printf("\n %d is Not a Prime Number", Number); } return 0; }

**The result of the above c program; as follows:**

Please Enter any number to Check for Prime :- 10

10 is Not a Prime Number

### C Program to Check Prime Number Using While Loop

#include <stdio.h> int main() { int i = 2, Number, count = 0; printf("\n Please Enter any number to Check for Prime :- "); scanf("%d", &Number); while(i <= Number/2) { if(Number%i == 0) { count++; break; } i++; } if(count == 0 && Number != 1 ) { printf("\n %d is a Prime Number", Number); } else { printf("\n %d is Not a Prime Number", Number); } return 0; }

**The result of the above c program; as follows:**

Please Enter any number to Check for Prime :- 11

11 is a Prime Number

### C Program to Check Prime Number Using Function

#include <stdio.h> int Find_Factors(int Number) { int i, Count = 0; for (i = 2; i <= Number/2; i++) { if(Number%i == 0) { Count++; } } return Count; } int main() { int Number, count = 0; printf("\n Please Enter any number to Check for Prime :- "); scanf("%d", &Number); count = Find_Factors(Number); if(count == 0 && Number != 1 ) { printf("\n %d is a Prime Number", Number); } else { printf("\n %d is Not a Prime Number", Number); } return 0; }

**The result of the above c program; as follows:**

Please Enter any number to Check for Prime :- 31

31 is a Prime Number

## Be First to Comment