# C Program to Display Prime Numbers Between Intervals Using Function

In this tutorial, we’ll develop a C program that utilizes a function to display prime numbers within a specified range. The program will prompt the user to input the lower and upper limits of the interval, and then it will identify and display all the prime numbers within that range. We’ll provide a step-by-step explanation of the algorithm, the C code implementation, and examples of the program’s output.

## Algorithm for Displaying Prime Numbers Between Intervals

1. Input: Accept the lower and upper limits of the interval from the user.
2. Function Definition: Create a function, say `isPrime`, to check if a given number is prime.
• The function should return 1 if the number is prime and 0 otherwise.
3. Loop Through the Interval:
• Iterate through each number in the specified range.
• For each number, call the `isPrime` function to check if it’s prime.
• If the number is prime, display it.
4. Output: Display the prime numbers within the specified interval.

## C Program for Displaying Prime Numbers Between Intervals

``````#include <stdio.h>

// Function to check if a number is prime
int isPrime(int num) {
if (num < 2) {
return 0; // Numbers less than 2 are not prime
}
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0) {
return 0; // If divisible by any number from 2 to sqrt(num), not prime
}
}
return 1; // If not divisible by any number, prime
}

int main() {
int lower, upper;

// Input the interval limits
printf("Enter the lower limit of the interval: ");
scanf("%d", &lower);
printf("Enter the upper limit of the interval: ");
scanf("%d", &upper);

// Display prime numbers within the interval using the isPrime function
printf("Prime numbers between %d and %d are:\n", lower, upper);
for (int i = lower; i <= upper; ++i) {
if (isPrime(i)) {
printf("%d\t", i);
}
}

return 0;
}
``````

## Output Example

### Example: Display Prime Numbers Between 10 and 50

``````Enter the lower limit of the interval: 10
Enter the upper limit of the interval: 50
Prime numbers between 10 and 50 are:
11      13      17      19      23      29      31      37      41      43      47
``````

## Explanation

1. Input: The user is prompted to enter the lower and upper limits of the interval (e.g., 10 and 50).
2. Function `isPrime`:
• The `isPrime` function checks if a given number is prime.
• It returns 1 if the number is prime and 0 otherwise.
• The function uses a loop to check divisibility from 2 to the square root of the number.
3. Loop Through the Interval:
• The program iterates through each number in the specified range.
• For each number, it calls the `isPrime` function to check if it’s prime.
• If the number is prime, it is displayed.
4. Output: The prime numbers within the specified interval (10 to 50) are displayed.

## Conclusion

This C program demonstrates how to use a function to identify and display prime numbers within a given interval. The `isPrime` function is essential for checking the primality of each number in the range. The program is flexible, allowing users to input different intervals for prime number identification. Feel free to run the program with other intervals and explore the prime numbers within those ranges. If you have any questions or need further clarification, please don’t hesitate to ask!