C program to display prime numbers between given interval has been shown here. For example, the prime numbers between 10 and 20 are 11, 13, 17, 19. In this program, the upper and lower bounds of the interval are taken as inputs.

#### Page contents:

1. Program & Output

## 1. C Program & output to display prime numbers between given interval

Code has been copied
/**********************************************************
alphabetacoder.com
C program to display prime numbers between given intervals
***********************************************************/

#include <stdio.h>
#include <math.h>

// function to check prime
int check_prime(int num) {
// declare variables
int i;
// no prime number less than 2, so return false
if (num < 2)
return 0;
// check divisibility of num
for (i = 2; i <= sqrt(num); i++) {
if (num % i == 0) {
return 0; // num is composite so return false
}
}
// num is prime so return true
return 1;
}

int main() {
// declare variables
int n1, n2, i;

// take input of the inteval
printf("Enter the lower and upper bounds of interval = ");
scanf("%d %d", & n1, & n2);

printf("Prime numbers between %d and %d: ", n1, n2);
// find primes between n1 and n2
for (i = n1; i <= n2; i++) {
// check if current number is prime
if (check_prime(i)) {
printf("%d ", i);
}
}

return 0;
}


Output

Enter the lower and upper bounds of interval = 10 30

Prime numbers between 10 and 30: 11 13 17 19 23 29