In this example, We Check inputted number is prime or not and print prime numbers between range

### What is Prime Number?

A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime because the only ways of writing it as a product, 1 × 5 or 5 × 1, involve 5 itself.

## C Program to Check Whether a Number is Prime or Not Using Loop

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

Output :

```	```
Enter the number to check prime:87
Number is not prime
```
```

## C Program to Check Whether a Number is Prime or Not Using User Define Function

```	```
#include <stdio.h>
int prime(int n);
int main()
{
int num,res=0;
printf("\nENTER A NUMBER: ");
scanf("%d",&num);
res=prime(num);
if(res==0)
printf("Number is prime");
else
printf("Number is not prime");
return 0;
}
int prime(int n)
{
int i;
for(i=2;i<=n/2;i++)
{
if(n%i!=0)
continue;
else
return 1;
}
return 0;
}
```
```

Output :

```	```
ENTER A NUMBER: 869
Number is not prime
```
```

## C Program to Check Whether a Number is Prime or Not Using Recursion

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

int prime(int num, int i)
{
if (i == 1)
{
return 1;
}
else
{
if (num % i == 0)
{
return 0;
}
else
{
return prime(num, i - 1);
}
}
}
```
```

Output :

```	```
Enter a number: 29
29 is a prime number
```
```

## C Program to Display Prime Numbers Between Two Intervals

```	```
#include <stdio.h>

int main() {
int start, end, i, flag;
printf("Enter Starting Point :: ");
scanf("%d", &start);

printf("Enter Ending Point :: ");
scanf("%d", &end);

printf("Prime numbers between %d and %d are: ", start, end);

while (start < end) {
flag = 0;

if (start <= 1) {
++start;
continue;
}
for (i = 2; i <= start / 2; ++i) {
if (start % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d ", start);
++start;
}
return 0;
}
```
```

Output :

```	```
Enter Starting Point :: 1
Enter Ending Point :: 50
Prime numbers between 1 and 50 are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
```
```