Any natural number not divisible by other numbers except one and itself is called Prime Number in C. Let us see how to write a C Program to Find Prime Number using For Loop, While Loop, and Functions.

**TIP:** 2 is the only even prime number in C

**Prime Numbers:** 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, etc

## C Program to Find Prime Number Using For Loop

This prime number program allows the user to enter any integer value. Using this value, this prime number program in C will check whether the given number is a Prime number or Not using For Loop.

/* C Program to Find 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 \n"); 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; }

**OUTPUT**

Let me enter another Value for this prime number program in c

This time we will use Non-prime value

**ANALYSIS**

In this c prime number program, We initialized the integer i value to 1, and also (i <= Number) condition will help the For Loop to terminate when the condition fails.

for (i = 2; i <= Number/2; i++) { if(Number%i == 0) { count++; break; } }

Within the C Programming for loop, there is an If statement to check whether the Number divisible by i is exactly equal to 0 or not. If the condition is True, then Count will increment, and then Break Statement will execute.

Next, we used another If statement to check, whether Count is Zero and Number is Not equal to 1.

User entered integer in the above program to find prime number example is 11

**First Iteration: for(i = 2; i <= 11/2; i++)**

It means condition inside the For loop (2 <= 5.5) is True

Now, Check the if condition – if (11%2 == 0). As you know, the condition is False

i++ means i will become 3

**Second Iteration: for(i = 3; 3 <= 11/2; i++)**

It means condition inside the For loop (3 <= 5.5) is True

if (11 % 3 == 0) – condition is False

i++ means i will become 4

**Third Iteration: for(i = 4; 4 <= 11/2; i++)**

It means condition inside the For loop of C prime number program (4 <= 5.5) is True

if (11 % 4 == 0) – condition is False

i++ means i will become 5

**Fourth Iteration: for(i = 5; 5 <= 11/2; i++)**

It means condition inside the For loop (5 <= 5.5) is True

if (11 % 5 == 0) – condition is False

i++ means i will become 6

**Fifth Iteration: for(i = 6; 6 <= 11/2; i++)**

It means condition inside the For loop (5 <= 5.5) of C program to find prime number is False. So, compiler will come out of the For Loop.

Next, the compiler will enter in to If statement. ** if(count == 0 && Number != 1 ). **In all Five iterations, If statement failed, so Count Value has not incremented from initialized o. And the Number that we inserted is 11. So, the condition is True, which means Prime Number. So, 11 is a prime number.

## C Program to Find Prime Number Using While Loop

This program for prime numbers allows the user to enter any integer value. Next, this program will check a number is a Prime number or not using While Loop.

/* C Program to Find 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 \n"); 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; }

We just replaced the For loop in the above prime number program example with the While loop. If you don’t understand the While Loop, please refer While Loop article here: WHILE LOOP

## C Program to Find Prime Number Using Functions

This prime number program allows the user to enter any integer value. User entered value will be passed to the Function that we created. Within this User defined function, this C program find Factors of a number using For Loop.

/* C Program to Find Prime Number using Functions */ #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 i, Number, count = 0; printf("\n Please Enter any number to Check for Prime \n"); 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; }

**OUTPUT**

**ANALYSIS**

In this program for prime number in c, When the compiler reaches to Find_Factors(Number) line in the main() program, the compiler will immediately jump to below function:

int Find_Factors(int Number)

We already explained LOGIC in the above example. If you did not understand the functions, Please refer Functions in C article for a better understanding