C Program to Display Prime Numbers Between Two Intervals

Prime numbers are numbers greater than 1 that have no divisors other than 1 and themselves. In this article, we will explore various C Program to Display Prime Numbers Between Two Intervals. We’ll cover multiple approaches with examples, explanations, and outputs.

Prerequisites

Before diving into the examples, you should be familiar with:

  • Basic C syntax and structure.
  • Loops and conditional statements.
  • Functions in C.

Understanding Prime Numbers

A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. For example, 5 is prime because the only way to form it by multiplying two smaller natural numbers is 1 × 5 or 5 × 1.

Example 1: Using Simple Iteration

1.1 Explanation

The simplest way to find prime numbers between two intervals is to iterate through each number in the range and check if it is prime.

1.2 Program: Simple Iteration

C
#include <stdio.h>

int isPrime(int num) {
    if (num <= 1) return 0;
    for (int i = 2; i <= num / 2; i++) {
        if (num % i == 0) return 0;
    }
    return 1;
}

int main() {
    int low, high;
    printf("Enter two intervals: ");
    scanf("%d %d", &low, &high);

    printf("Prime numbers between %d and %d are: ", low, high);
    for (int i = low; i <= high; i++) {
        if (isPrime(i)) printf("%d ", i);
    }

    return 0;
}

1.3 Output

C
Enter two intervals: 10 50
Prime numbers between 10 and 50 are: 11 13 17 19 23 29 31 37 41 43 47

Example 2: Using Optimized Checking

2.1 Explanation

A more efficient way to check for prime numbers is to iterate only up to the square root of the number. If a number is not prime, it must have a factor less than or equal to its square root.

2.2 Program: Optimized Checking

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

int isPrime(int num) {
    if (num <= 1) return 0;
    for (int i = 2; i <= sqrt(num); i++) {
        if (num % i == 0) return 0;
    }
    return 1;
}

int main() {
    int low, high;
    printf("Enter two intervals: ");
    scanf("%d %d", &low, &high);

    printf("Prime numbers between %d and %d are: ", low, high);
    for (int i = low; i <= high; i++) {
        if (isPrime(i)) printf("%d ", i);
    }

    return 0;
}

2.3 Output

C
Enter two intervals: 10 50
Prime numbers between 10 and 50 are: 11 13 17 19 23 29 31 37 41 43 47

Example 3: Using a Separate Function for Interval Checking

3.1 Explanation

To make the code more modular, we can create a separate function to print prime numbers between the given intervals.

3.2 Program: Separate Function for Interval Checking

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

int isPrime(int num) {
    if (num <= 1) return 0;
    for (int i = 2; i <= sqrt(num); i++) {
        if (num % i == 0) return 0;
    }
    return 1;
}

void printPrimes(int low, int high) {
    printf("Prime numbers between %d and %d are: ", low, high);
    for (int i = low; i <= high; i++) {
        if (isPrime(i)) printf("%d ", i);
    }
}

int main() {
    int low, high;
    printf("Enter two intervals: ");
    scanf("%d %d", &low, &high);

    printPrimes(low, high);

    return 0;
}

3.3 Output

C
Enter two intervals: 10 50
Prime numbers between 10 and 50 are: 11 13 17 19 23 29 31 37 41 43 47

Example 4: Using Recursion

4.1 Explanation

We can also use recursion to check for prime numbers. Although recursion is not the most efficient way to solve this problem, it is useful for understanding different approaches.

4.2 Program: Recursion

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

int isPrime(int num, int i) {
    if (num <= 1) return 0;
    if (i == 1) return 1;
    if (num % i == 0) return 0;
    return isPrime(num, i - 1);
}

void printPrimes(int low, int high) {
    printf("Prime numbers between %d and %d are: ", low, high);
    for (int i = low; i <= high; i++) {
        if (isPrime(i, sqrt(i))) printf("%d ", i);
    }
}

int main() {
    int low, high;
    printf("Enter two intervals: ");
    scanf("%d %d", &low, &high);

    printPrimes(low, high);

    return 0;
}

4.3 Output

C
Enter two intervals: 10 50
Prime numbers between 10 and 50 are: 11 13 17 19 23 29 31 37 41 43 47

Conclusion

This article explored various C Program to Display Prime Numbers Between Two Interval. We discussed simple iteration, optimized checking using the square root, modular functions, and recursion. Each method has its unique advantages, and understanding these different approaches allows you to choose the most appropriate solution for your specific needs.

By mastering these techniques, you can apply similar logic to other programming challenges and enhance your problem-solving skills in C.