# C program to display prime numbers between two intervals using function

In this article, you will learn how to print the prime numbers between two intervals using the function in the c programming language.

You should have knowledge of the following topics in c programming to understand this program:

- C functions
- C

function**main** - C

loop statement**for** - C

condition statement**if** - C

statement**continue** - C

function**printf**

In this program, we are using a

function to find the prime numbers of the given two intervals.**user-defined**

### Source Code

```
// C program to display prime numbers between two intervals using function
#include <stdio.h>
// @custom function to find the prime numbers between intervals
int GeneratePrimeNumbers(int low_number, int high_number) {
int flag, temp;
// this will swap the value of intervals between each other
// if low_number is greater then high_number
if (low_number > high_number) {
temp;
temp = low_number;
low_number = high_number;
high_number = temp;
}
printf("\nThe prime numbers between %d and %d are: ", low_number, high_number);
for (int i = low_number; i <= high_number; i++) {
flag = 0;
// this will ignore numbers less than 2
if (i <= 1) {
continue;
}
// if low_number is a non-prime number, flag will be 1
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d ", i);
}
printf("\n");
return 0;
}
// @driver function to call & print the primer numbers
int main() {
int low_number, high_number;
printf("Enter the two intervals:\n");
scanf("%d %d", &low_number, &high_number);
// @call the custom function to print the numbers
GeneratePrimeNumbers(low_number, high_number);
return 0;
}
```

### Output

`Enter the two intervals:`

16

35

`The prime numbers between 16 and 35 are: 17 19 23 29 31`

**Note**: We swapped the value of the interval between each other if

is greater than **low_number**

.**high_number**

### Explanation

In this given program we used a **user-defined** function to find the primer numbers between two intervals.

so first we have taken two inputs

and **16**

from the user then these values pass to this function **35**

after making the calculation it returned the list of prime numbers.**GeneratePrimeNumbers()**