#### C Program to print Prime Numbers between 2 numbers

Do you know what is prime? Ok here we are every number that is only divisible by 1 or itself is prime so

we will write a C Program to print Prime Numbers between 2 numbers or interval that should be entered by User

**This first program is simple here we are doing it in simple way
**

```
#include <stdio.h>
int main()
{
int low, high, i, flag;
printf("Enter two numbers(intervals): ");
// user can enter 2 value like 2 to 50
scanf("%d %d", &low, &high);
printf("Prime numbers between %d and %d are: ", low, high);
// here we check that number must be greater than 2
if(low<2)
low++;
while (low < high)
{
flag = 0;
// flag will do 0 and 1, 1 for not prime and 0 for prime
for(i = 2; i <low; ++i)
{
// checking if number is divisible by small or not
if(low % i == 0)
{
flag = 1;
break;
}
}
//print if not
if (flag == 0)
printf("%d ", low);
++low;
}
return 0;
}
```

But the program may takes much time complexity O(n*n) approx

##### Second program is small here we run the loop for n/2 times

```
#include <stdio.h>
int main()
{
int a, b, i, flag;
printf("Enter two numbers(intervals): please try to enter from at least 2 ");
scanf("%d %d", &a, &b);
printf("aPrime numbers between %d and %d are: ", a, b);
if(a<2)
a++;
while (a< b)
{
flag = 0;
for(i = 2; i <= a/2; ++i)
{
if(a% i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
printf("%d ", a);
++a;
}
return 0;
}
```

This program is readable and so good with algo here complexity time is now O(n) approx

```
#include <stdio.h>
#include <math.h>
int main()
{
int a, b, i, flag;
printf("Enter two numbers(intervals): please try to enter from at least 2 ");
scanf("%d %d", &a, &b);
printf("Prime numbers between %d and %d are: ", a, b);
if(a<2)
a++;
while (a< b)
{
flag = 0;
for(i = 2; i <=sqrt(a); ++i)
{
if(a% i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
printf("%d ", a);
++a;
}
return 0;
}
```