# Prime Number Program in C Language 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.

• Prime Number Program in C using for loop
• C Program to Print all Prime Numbers from 1 to N
• Prime Number Program in C using function
• Prime Number Program in C using recursion

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

## 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.

## 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 praveen171086@gmail.com.

## You can also visit my Hindi YouTube Channel. I am Avinash Singh, a software engineer from the profession and have been working in the IT industry since 2009. I live in Barabanki district of Uttar Pradesh in India and completed intermediate in 2001 from Ram Sevak Inter College, a UP Board school. After inter I completed my professional degree (BCA & MCA) from Indira Gandhi National Open University, Delhi in 2008 and along with it I did many certifications to keep my knowledge up to date and the next year I started a software development company E – Vision Technocraft Pvt Ltd.

### Related Post ## What is C language and how to learn?What is C language and how to learn?

There are many questions in the mind of many about what is C Language and how to learn C Language. If you want to become a good Software Engineer, then ## Install CodeBlocks CompilerInstall CodeBlocks Compiler

In this tutorial we will talk about C compiler and understand what is compiler and how you can install CodeBlocks compiler to compile and run C programs. ## Type Casting in C ProgrammingType Casting in C Programming

In this post we will talk about Data Type Casting in C Programming. Along with this, we will also understand the difference between implicit type casting and explicit type casting.