Any natural number not divisible by other numbers except one and itself is called Prime Number. Let us see how to write a C Program to Find Prime Number using For Loop, While Loop, and Functions.
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. 2 is the only even.
C Program to Find Prime Number Using For Loop
This program allows the user to enter any integer value. Using this value, this program will check or find whether the given number is a Prime number or Not using For Loop.
#include <stdio.h>
int main()
{
int i, Number, count = 0;
printf("\n Please Enter any number to Check = ");
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", Number);
}
return 0;
}

Let me enter another Value for this example
Please Enter any number to Check = 199
199 is a Prime Number
This time we will use a different one
Please Enter any number to Check = 365
365 is Not
In this C Program to Find Prime Number, We initialized the integer i value to 1, and also (i <= Number) condition will help the C For Loop to terminate when the condition fails.
Within the C Programming for loop, there is an C If statement to check whether divisible by i is exactly equal to 0 or not. If the condition is True, the Count will increment, and the C break Statement will execute.
Next, we used another If statement to check whether the Count is Zero and not equal to 1.
Please Enter any Value
11
11 is a Prime
The user entered an integer in the above C program is 11.
First Iteration: for(i = 2; i <= 11/2; i++)
It means the 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 the 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 the condition inside the For loop of the C program to find the prime number (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 the 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 the condition inside the For loop (5 <= 5.5) is False. So, the compiler will come out of the For Loop.
Next, the compiler will enter the If statement. if(count == 0 && Number!= 1 ). Count Value has not incremented from initialized o in all five iterations if the statement failed. And the Number that we inserted is 11. So, the condition is True, which means 11 is a prime.
Second approach of Prime Number using for loop
In this example, the for loop checks up to the square root of the given number.
#include <stdio.h>
int main()
{
int Number, res = 1;
printf("Enter a positive integer = ");
scanf("%d", &Number);
if (Number <= 1)
{
res = 0;
}
else
{
for (int i = 2; i * i <= Number; i += 2)
{
if (Number % i == 0)
{
res = 0;
break;
}
}
}
if (res)
printf("%d is a prime number", Number);
else
printf("%d is not a prime number", Number);
}
Enter a positive integer = 22
22 is not a prime number
Enter a positive integer = 151
151 is a prime number
C Program to Find Prime Number Using While Loop
This c program allows the user to enter any integer value. Next, this program will check or find whether a number is Prime or not using While Loop.
#include <stdio.h>
int main(void)
{
int i = 2, Num, count = 0;
printf("Please Enter any Value = ");
scanf("%d", &Num);
while(i <= Num/2)
{
if(Num%i == 0)
{
count++;
break;
}
i++;
}
if(count == 0 && Num != 1 )
{
printf("\n%d is a Prime", Num);
}
else
{
printf("\n%d is Not", Num);
}
return 0;
}
We just replaced the For in the above program example with the While loop. If you don’t understand the While Loop, please refer to the C While Loop article.

C Program to check a prime number using a do-while loop
The following program uses a do-while loop iteration process to check whether the given number is a prime number. Refer to the c do while loop.
#include <stdio.h>
int main()
{
int Number, i = 2, res = 0;
printf("Enter a positive integer = ");
scanf("%d", &Number);
if (Number <= 1)
{
res = 1;
}
else
{
do
{
if (Number % i == 0)
{
res = 1;
break;
}
i++;
} while (i <= Number / 2);
}
if (res == 0)
printf("%d is a prime number", Number);
else
printf("%d is a not a prime number", Number);
}
Enter a positive integer = 199
199 is a prime number
C Program to Find Prime Number Using Functions
This c program allows the user to enter any integer value. The user-entered value will be passed to the Function that we created. Within this User defined function, this program finds Factors of using For Loop and the if statement.
#include <stdio.h>
int Find_Fact(int nm)
{
int i, Count = 0;
for (i = 2; i <= nm/2; i++)
{
if(nm%i == 0)
{
Count++;
}
}
return Count;
}
int main(void)
{
int nm, count = 0;
printf("\n Please Enter any to Check \n");
scanf("%d", &nm);
count = Find_Fact(nm);
if(count == 0 && nm != 1 )
{
printf("\n %d is a Prime", nm);
}
else
{
printf("\n %d is Not", nm);
}
return 0;
}

In this program, When the compiler reaches to Find_Fact line in the main() program, the compiler will immediately jump to the below function:
int Find_Fac(int nm)
We already explained the LOGIC inside this function in the above example. If you did not understand the functions, Please refer to the Functions in C article for a better understanding.
Prime number using functions example 2
The following example shows another way of finding a prime number using a for loop. It skips even numbers completely and checks odd numbers to find prime numbers.
#include <stdio.h>
int isPrime(int n)
{
int res = 1;
if (n <= 1)
res = 0;
else if (n == 2)
res = 1;
else if (n % 2 == 0)
res = 0;
else
{
for (int i = 3; i * i <= n; i += 2)
{
if (n % i == 0)
{
res = 0;
break;
}
}
}
return res;
}
int main()
{
int Number;
printf("Enter a positive integer = ");
scanf("%d", &Number);
if (isPrime(Number))
printf("%d is a prime number", Number);
else
printf("%d is a not a prime number", Number);
}
Enter a positive integer = 32
32 is not a prime number
Enter a positive integer = 53
53 is a prime number
Program to find a prime number using recursion
This example checks whether a given integer is a prime number using recursion. Here, we define a recursive function that accepts the actual number and half of it. Refer to the recursion in c.
#include <stdio.h>
int isPrime(int n, int i)
{
if (i == 1)
return 1;
else
{
if (n % i == 0)
return 0;
else
return isPrime(n, i - 1);
}
}
int main()
{
int Number, res = 1;
printf("Enter a positive integer = ");
scanf("%d", &Number);
if (Number <= 1)
{
printf("Enter number greater than 1.");
}
else
{
if (isPrime(Number, Number / 2))
printf("%d is a prime number", Number);
else
printf("%d is a not a prime number", Number);
}
}
Enter a positive integer = 113
113 is a prime number
Also Read
Comments are closed.