In this post, we will make the Prime number program in c. It will help us to check whether the number is prime or not.

Prime number: A number not divisible by any other number except one is called a prime number.

E.g. 2,3,5,7,11 etc.

Prime number program in C using for loop

In this program, we have used a function to check the Prime number in C. Return type of this function is bool type.

It will return true if the number is prime and false if it is not.

In the “isPrime” function, we checked that this number is divisible by any number smaller than or equal to the square root of this number “n” using for loop.

The Time complexity of this program is O(sqrt(n)).[Because we are running a loop from 2 to the square root of n]

```#include<stdio.h>
#include<stdbool.h>
bool isPrime(int n){
if(n==1)
return false;
//for loop
for(int i=2;i*i<=n;i++){
if(n%i==0)
return false;
}
return true;
}
int main() {
int n;
printf("n Enter a number:");
scanf("%d",&n);
if(isPrime(n)==1)
printf("n %d is a Prime Number",n);
else
printf("n %d is not a Prime Number",n);
return 0;
}
```
Output:

Enter a number:573
573 is not a Prime Number

Prime number program in c using while loop

In this program, we will check whether the number is prime or not using a while loop.

```#include<stdio.h>
int main()
{
int num,i=2;
printf("n Enter a number:");
scanf("%d",&num);
while(i<num)
{
if(num%i==0)
break;
i++;
}
if(i==num)
printf("\n %d is a Prime Number",num);
else
printf("\n %d is not a Prime Number",num);
return 0;
}
```

Approach to check number entered by the user is a prime number or not using the while loop:

• We will declare two integer numbers one is num, and another is i.
• We will initialize “i” with two, and the user enters the value of num.
• In a while loop, we will perform num modulus “i” where i is less than num.
• If num%i comes to zero, it means that the number entered by the user is not a prime number.
• If the value of num is equal to “i” then it is a prime number.

C program to check whether a number is prime or not using a do-while loop

```#include<stdio.h>
int main()
{
int num,i=2;
printf("\n Enter a number:");
scanf("%d",&num);
do
{
if(num%i==0)
{
break;
}
i++;
}while(i<num);
if(i==num)
printf("\n %d is a prime number",num);
else
printf("\n %d is not a prime number",num);

return 0;
}
```

Approach to determine whether the number is prime or not using a do-while loop:

• We declare an integer number and initialize its value from the user.
• We are using a do-while loop to check whether the number is prime or not.
• Use the if statement and modulus operator (it gives the remainder when we divide two numbers) to compare the remainder with zero; if the remainder comes to zero, the number is divisible by another number. Hence, it is not a prime number because it is divisible by another number.
• We print it is not the prime number.
• If the value of i equals the number entered by the user, then we will print it as a prime number.

Write a program to check if a given number is prime or not using for loop.

```#include <stdio.h>
int main()
{
int num, i;

printf("\n Enter a number:");
scanf("%d", &num);
for (i = 2; i < num; i++)
{
if (num % i == 0)
{
printf("\n %d is not a prime number", num);
break;
}
}
if (i == num)
printf("\n %d is a prime number", num);

return 0;
}
```

Output:
Enter a number:11
11 is a prime number

Approach to determine whether the number is prime or not using for loop:

• First, we declare an integer number.
• Initialize its value from the user using scanf.
• Now we are using for loop to divide that number from all those below that number and check the remainder.
• Use the if statement to check the remainder. If the remainder comes to zero, the number is divisible by another number.
• We have to print that the number entered by the user is not the prime number.
• If the value of i equals the number entered by the user, then we will print it as a prime number.