# C Program to Display Prime Numbers Between Two Intervals

In this blog post, we will explore a C program that calculates and displays prime numbers within a specified range of intervals. The program will provide readers with a step-by-step understanding of the logic and implementation, offering insights into fundamental concepts such as loops, conditionals, and prime number generation.

## Introduction

Prime numbers are natural numbers greater than 1 that have no positive divisors other than 1 and themselves. Generating prime numbers within a given range is a common mathematical and programming problem. This C program aims to efficiently find and display all prime numbers between two user-defined intervals.

## Program Logic

The logic of the program involves iterating through each number within the specified range and checking if it is a prime number. Here’s a breakdown of the key steps:

1. User Input:

• The program prompts the user to enter the lower and upper bounds of the interval.
2. Prime Number Check:

• For each number in the interval, the program checks if it is a prime number.
• A number `n` is considered prime if it has no divisors other than 1 and `n` itself.
• To check for divisibility, the program iterates from 2 to the square root of `n`.
3. Display Prime Numbers:

• Prime numbers found within the specified range are displayed to the user.

## C Program Code

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

// Function to check if a number is prime
int isPrime(int num) {
if (num < 2) {
return 0; // Not a prime number
}
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) {
return 0; // Not a prime number
}
}
return 1; // Prime number
}

// Function to display prime numbers in an interval
void displayPrimes(int lower, int upper) {
printf("Prime numbers between %d and %d are:\n", lower, upper);
for (int i = lower; i <= upper; i++) {
if (isPrime(i)) {
printf("%d\n", i);
}
}
}

// Main function
int main() {
int lower, upper;

// User input for interval
printf("Enter the lower bound of the interval: ");
scanf("%d", &lower);
printf("Enter the upper bound of the interval: ");
scanf("%d", &upper);

// Display prime numbers
displayPrimes(lower, upper);

return 0;
}
``````

## Program Output

Let’s consider an example where the user enters the interval [10, 50]. The program output will display the prime numbers within this range:

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

## Conclusion

This C program serves as a practical example of generating and displaying prime numbers within a specified interval. Understanding the logic behind prime number generation contributes to foundational programming skills and lays the groundwork for more complex mathematical algorithms.

As readers explore the code, they can modify the input intervals, experiment with additional optimizations, or incorporate the prime number logic into larger C programs. The ability to work with prime numbers is valuable in various areas of computer science, including cryptography, number theory, and algorithm design.

Feel free to engage with the code, run it with different intervals, and use it as a basis for further exploration of C programming concepts. Happy coding!

# Example #2: Display Prime Numbers Between Two Intervals When Larger Number is Entered First

In this blog post, we will explore a C program that finds and displays prime numbers within a user-defined range of intervals. However, in this example, we’ll consider a scenario where the user enters the larger number first. The program will handle this input and efficiently determine the prime numbers within the specified range.

## Introduction

Generating prime numbers between two intervals is a common programming task. This C program builds upon the previous example but introduces a user-friendly approach. It accommodates situations where the user inputs the larger number before the smaller one, providing a flexible and intuitive user experience.

## Program Logic

The program’s logic remains focused on identifying prime numbers within the specified range. The key steps include:

1. User Input:

• The program prompts the user to enter the upper and lower bounds of the interval.
• To ensure flexibility, the program identifies the larger and smaller numbers among the inputs.
2. Prime Number Check:

• Similar to the previous example, the program checks each number in the interval for primality.
• The prime-checking logic involves iterating from 2 to the square root of the number.
3. Display Prime Numbers:

• The prime numbers found within the specified range are displayed to the user.

## C Program Code

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

// Function to check if a number is prime
int isPrime(int num) {
if (num < 2) {
return 0; // Not a prime number
}
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) {
return 0; // Not a prime number
}
}
return 1; // Prime number
}

// Function to determine larger and smaller numbers
void determineBounds(int num1, int num2, int *lower, int *upper) {
if (num1 < num2) {
*lower = num1;
*upper = num2;
} else {
*lower = num2;
*upper = num1;
}
}

// Function to display prime numbers in an interval
void displayPrimes(int lower, int upper) {
printf("Prime numbers between %d and %d are:\n", lower, upper);
for (int i = lower; i <= upper; i++) {
if (isPrime(i)) {
printf("%d\n", i);
}
}
}

// Main function
int main() {
int num1, num2, lower, upper;

// User input for interval
printf("Enter the larger number of the interval: ");
scanf("%d", &num1);
printf("Enter the smaller number of the interval: ");
scanf("%d", &num2);

// Determine bounds
determineBounds(num1, num2, &lower, &upper);

// Display prime numbers
displayPrimes(lower, upper);

return 0;
}
``````

## Program Output

Let’s consider an example where the user enters the interval [40, 20]. The program efficiently determines the larger and smaller numbers and displays the prime numbers within this range:

``````Enter the larger number of the interval: 40
Enter the smaller number of the interval: 20
Prime numbers between 20 and 40 are:
23
29
31
37
``````

## Conclusion

This example demonstrates how the C program can gracefully handle scenarios where the user inputs the larger number before the smaller one. The logic for prime number generation remains consistent, providing users with a seamless experience.

As readers explore the code, they can experiment with different input intervals, further optimizing or extending the program. Understanding how to adapt programs to various user inputs is a valuable skill in software development. Feel free to engage with the code, modify inputs, and use it as a foundation for exploring additional C programming concepts. Happy coding!