# C Program to Display Prime Numbers Between Intervals Using Function

In this post, we will learn how to display prime numbers between intervals using functions in the C Programming language.

As we know, Prime Numbers are the whole numbers greater than 1 and have only two factors – 1 and itself. For example: 2, 3, 5, 7 and so on.

We will be writing a program which will display prime numbers lying in a given range using functions.

So, without further ado, let’s begin this tutorial.

## C Program to Display Prime Numbers Between Intervals Using Function

```// C Program to Display Prime Numbers Between Intervals Using Function
#include <stdio.h>
int checkPrime(int n);
int main(){
int lowerlimit, upperlimit, i, flag;

printf("Enter the lower limit: ");
scanf("%d", &lowerlimit);
printf("Enter the upper limit: ");
scanf("%d", &upperlimit);

printf("Prime Numbers Between %d and %d are: \n", lowerlimit, upperlimit);

for (i = lowerlimit + 1; i < upperlimit; ++i){
flag = checkPrime(i);
if (flag ==  1){
printf("%d\t", i);
}
}
return 0;
}

int checkPrime(int n){
int j, flag = 1;
for (j = 2; j <= n / 2; ++j){
if (n % j == 0){
flag = 0;
break;
}
}
return flag;
}
```

Output

``````Enter the lower limit: 5
Enter the upper limit: 25
Prime Numbers Between 5 and 25 are:
7	11	13	17	19	23	``````

## How Does This Program Work ?

``````    int lowerlimit, upperlimit, i, flag;
``````

In this program, we have declared four int data type variables named as lowerlimit, upperlimit, i and flag.

``````    // Asking for Input
printf("Enter the lower limit: ");
scanf("%d", &lowerlimit);
printf("Enter the upper limit: ");
scanf("%d", &upperlimit);
``````

Then, the user is asked to enter the range, for example: the lower and upper limit of the range.

``````int checkPrime(int n){
int j, flag = 1;
for (j = 2; j <= n / 2; ++j){
if (n % j == 0){
flag = 0;
break;
}
}
return flag;
}
``````

We have defined a custom function named checkPrime which will check whether a particular value is prime or not.

``````    for (i = lowerlimit + 1; i < upperlimit; ++i){
flag = checkPrime(i);
if (flag ==  1){
printf("%d\t", i);
}
}
``````

We import this custom function in the main function which checks whether different iterations of i are prime or not.

If yes, then we will print it otherwise we will just ignore it.

## Conclusion

I hope after going through this post, you understand how to display prime numbers between intervals using functions in C Programming language.

If you have any doubt regarding the problem, feel free to contact us in the comment section. We will be delighted to help you.