# C Program to Display Prime Numbers Between Two Intervals

To understand this example, you should have knowledge of following C programming topics:

This program asks user to enter two integers and this program will display all prime numbers between these intervals. If you don't know how to check whether a number is prime or not then, this program may seem little bit complex. You can visit this page to learn about prime numbers and how to check whether a number is prime or not in C programming.

## Source Code to Display Prime Numbers Between two Intervals

```
/* C program to display all prime numbers between Two interval entered by user. */
#include <stdio.h>
int main()
{
int n1, n2, i, j, flag;
printf("Enter two numbers(intevals): ");
scanf("%d %d", &n1, &n2);
printf("Prime numbers between %d and %d are: ", n1, n2);
for(i=n1+1; i<n2; ++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;
}
```

**Output**

Enter two numbers(intervals): 20 50 Prime numbers between 20 and 50 are: 23 29 31 37 41 43 47

In this program, it is assumed that, the user always enters smaller number first. This program will not perform the task intended if user enters larger number first. You can add the code to swap two numbers entered by user if user enters larger number first to make this program work properly.

Visit this page to learn, how you can display all prime numbers between two intervals by making user-defined function.