In this post of C programming language for prime number program in c language, we will create C programs to print (find) prime numbers in different ways.

As you know, Prime Numbers are those whole numbers which are not divisible by only the number 1 and any other number other than itself.

In other words, prime numbers are said to be those numbers in which if you divide the number 1 and itself by any number other than that number, then the remainder (remainder) does not come to 0.

Prime Numbers Example:

2, 3, 5, 7, 11, 13, 17, 19

Apart from this, those numbers which are divisible by number 1 and other numbers other than ourselves, we call them composite numbers but number 1 is neither prime number nor composite number.

You can make a program to find or print prime numbers in C programming in many ways and therefore we will also make C programs to print prime numbers by taking number input from the user in the 4 ways given below.

To understand all the programs given in this tutorial, you must first be aware of for loop, functions and recursion.

Contents

- 1 What is the logic of Prime Number Check?
- 2 You can also read: Data Types in C Programming
- 3 Prime Number Program in C using for loop
- 4 Output 1:
- 5 Output 2:
- 6 Explanation:
- 7 C Program to Print all Prime Numbers from 2 to N
- 8 Output:
- 9 Prime Number Program in C using Function
- 10 Output 1:
- 11 Output 2:
- 12 Prime Number Program in C using Recursion
- 13 Output 1:
- 14 Output 2:
- 15 You can also visit my Hindi YouTube Channel.

## What is the logic of Prime Number Check?

Native Approach means a simple and common way of doing something. Let us now understand how we will find prime number.

As I mentioned above, Prime Numbers are those whole numbers which are not divisible by only the number 1 and any other number other than itself.

Therefore, in the number you want to check whether it is a prime number or not, except the number 1 and that number itself, all the numbers from number 2 to the half number (N/2) of that N number are one by one. By dividing.

We divide the number N only up to its half number (n/2) because the largest number that can divide any N number is the half number of that N number.

If the number N becomes completely divisible by any number, then it means that the number N is not a prime number. Let us understand this logic as the form of programs.

## You can also read: Data Types in C Programming

## Prime Number Program in C using for loop

In this program, we will take a number input from the user and then check and print whether that number is prime number or not.

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

**Prime Number Program in C Language**

## Output 1:

Enter a number : 12 12 is not a prime number

## Output 2:

Enter a Number : 7 7 is a prime number

## Explanation:

We have initialized the value of variable flag above to 0 and if any value of variable i inside the loop divides the value of variable n then the value of variable flag will change.

If the value of the variable flag changes, it means that the variable n is divisible by number 1 and any number other than itself.

Note:

You can keep the name of the variable in place of the flag.

## C Program to Print all Prime Numbers from 2 to N

In this program, we will input a number in variable n from the user and then find and print all prime numbers from number 2 to variable n.

```
#include<stdio.h>
int main()
{
int i,j,n,flag;
printf("Enter a number : ");
scanf("%d",&n);
printf("All prime numbers from 2 to %d : ",n);
for(i=2;i<=n;i++)
{
flag=0;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("%d ",i);
}
}
return 0;
}
```

**Prime Number Program in C Language**

## Output:

Enter a number : 18 All prime numbers from 2 to 18 : 2 3 5 7 11 13 17

## Prime Number Program in C using Function

```
#include<stdio.h>
int isPrime(int t);
int main()
{
int n,p;
printf("Enter a number : ");
scanf("%d",&n);
p=isPrime(n);
if(p==1)
{
printf("%d is a prime number",n);
}
else
{
printf("%d is not a prime number",n);
}
return 0;
}
int isPrime(int t)
{
int i;
for(i=2;i<=t/2;i++)
{
if(t%i==0)
{
return 0;
}
}
return 1;
}
```

**Prime Number Program in C Language**

## Output 1:

Enter a number : 12 12 is not a prime number

## Output 2:

Enter a Number : 7 7 is a prime number

## Prime Number Program in C using Recursion

```
#include<stdio.h>
int isPrime(int t,int i);
int main()
{
int n,p;
printf("Enter a number : ");
scanf("%d",&n);
p=isPrime(n,n/2);
if(p==1)
{
printf("%d is a prime number",n);
}
else
{
printf("%d is not a prime number",n);
}
return 0;
}
int isPrime(int t,int i)
{
if(i==1)
{
return 1;
}
else if(t%i==0)
{
return 0;
}
else
{
return isPrime(t,i-1);
}
}
```

**Prime Number Program in C Language**

## Output 1:

Enter a number : 12 12 is not a prime number

## Output 2:

Enter a Number : 7 7 is a prime number

I hope that you have liked this post. If you liked the post, then please share it with your friends and relatives and you can send your suggestion in the comment box below or email me at pr***********@gm***.com.