In this post, we are going to make the Prime number program in c in a 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 which 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 program in c using while loop

In this program, we are going to check whether the number is prime or not using 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:

Enter a number:6

6 is not a prime number

Enter a number:11

11 is a prime number

Output:

Enter a number:12

12 is not a prime number

This post is written by

If you are having issues in 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

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

- We will declare two integer number 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 equals to i then it is a prime number.

## Prime number program in c 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 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 to the number entered by the user then we will print it is a prime number.

## Prime number program in c 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 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.

**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 to the number entered by the user but the test condition is i<num so controls come out from the for loop.**__kind attention:__## 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 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.

**About this post:**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 program to check whether the number is prime or not which is below all program output.If you are having issues in 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

## Post a Comment

## Post a Comment