A natural number that is not divisible by any other number except 1 and itself is called as Prime Number. In this article, we will show you, 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

**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 C program allows the user to enter any integer value. Using this value, this program 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

This time we will use Non-prime value

**ANALYSIS**

We initialized the integer i value to 1 and also (i <= Number) condition will help the loop to terminate when the condition fails.

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

Within the for loop, there is 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 be incremented and then Break Statement will be executed. 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 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 (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) is False. So, compiler will come out of the For Loop.

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

## C Program to Find Prime Number Using While Loop

This program allows the user to enter any integer value. Using this value, this program will check the 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 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 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 program will 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**

When the compiler reaches to Find_Factors(Number) line in main() program then 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

Thank you for Visiting Our Blog