# Prime Number Program in C using while loop

In this post, we are going to make the Prime number program in c in the very simplest way to understand using while, for, and do-while loop. At last, I made a program to check whether the number is prime or "not" using function.

Prime number: A number that is not divisible by any other number except one and itself. 2 is the smallest prime number.
E.g. 2,3,5,7,11 etc.

## Prime number in C

In this program, we have used a function to check Prime number in C. Return type of this function is bool type. It will return 1, if number is prime and 0, if number is not prime. In "isPrime" function, we checked that this number is divisible by any number smaller than or equal to square root of this number "n".

```#include<stdio.h>#include<stdbool.h>
bool isPrime(int n){
if(n==1)
return false;

for(int i=2;i*i<=n;i++){
if(n%i==0)
return false;
}
return true;
}
int main() {
int n;
scanf("%d",&n);
printf("%d",isPrime(n));
return 0;
}
```
Output:9
0

Recommended: Factorial Program in c using for loop

## Prime number program in c using while loop

In this program, we are going to 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){printf("\n %d is not a prime number",num);break;}i++;}if(i==num)printf("\n %d is a prime number",num);return 0;}`
Output: ### Approach to check number entered by the user is a prime number or not using while loop:

• We will declare two integer numbers one is num and another is i.
• we will initialize i with 2 and the value of num is entered by the user.
• Using a while loop, we will compare the remainder when num is divided by all those numbers which are lesser than that number.
• if num%i comes to zero then we will print 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 do-while loop

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

Output:

Enter a number:6
6 is not a prime number

### 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 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, it means that 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>#include<conio.h>int main(){int num,i;clrscr();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);getch();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 numbers which are below that number and check the remainder.
• Use if statement to check the remainder. If the remainder comes to zero, it means that the number is divisible by another number.
• We print 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 is a prime number.

kind attention: In for loop, the value of i is incremented after executing the body of the loop so that’s why the value of i equals the number entered by the user but the test condition is i<num so controls come out from the for loop.

## Prime number program in c using function

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

Output:
Enter a number:12
12 is not a prime number

### Approach to determine whether the number is prime or not using functions:

• We declare an integer number and initialize its value from the user in the main function.
• We call the prime function.
• In the prime function, we declare int i=2.
• Using a while loop, we will check number is prime or not.
• We will compare the remainder with zero when we divide that number with all those numbers which are lesser than that number and if the remainder is zero condition is true and the function will print it is not a prime number.
• Else it will print it is a prime number.
This post is written by Coding Wallah. It is a little bit difficult program but I made this program in the very easiest way. So, you can understand easily. You can also read an approach to make a Prime Number Program in C to check whether the number is prime or not which is below all program output.

If you are having issues with these programs please comment below.

Recommended post:
Odd and even numbers program in c
Simple calculator program in c using switch case
Matrix program in c using array • Hackerrank 5* • Full Stack Developer • Masters in SEO