C Program to Display Prime Numbers Between Intervals Using Function

In C programming, prime numbers are those that have only two distinct positive divisors: 1 and the number itself. This article will guide you through writing a C Program to Display Prime Numbers Between Intervals Using Function. We’ll cover several variations and solutions, each with detailed explanations and outputs.

Prerequisites

Before we start, ensure you have:

  • Basic knowledge of C programming.
  • Understanding of functions in C.
  • A C compiler installed (e.g., GCC).

Understanding Prime Numbers

Prime numbers are natural numbers greater than 1 that have no positive divisors other than 1 and themselves. For example, 2, 3, 5, and 7 are prime numbers.

Example 1: Simple Prime Number Checker Using Function

1.1 Explanation

This program uses a function to check if a number is prime and then prints all prime numbers in a given interval.

1.2 Program: Simple Prime Number Checker

C
#include <stdio.h>

int isPrime(int num);

int main() {
    int start, end, i;

    printf("Enter two numbers (intervals): ");
    scanf("%d %d", &start, &end);

    printf("Prime numbers between %d and %d are: ", start, end);
    for(i = start; i <= end; i++) {
        if(isPrime(i))
            printf("%d ", i);
    }
    
    return 0;
}

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

1.3 Output

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

Example 2: Optimized Prime Number Checker Using Function

2.1 Explanation

This version optimizes the prime checking function by reducing the number of iterations and checking divisibility only up to the square root of the number.

2.2 Program: Optimized Prime Number Checker

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

int isPrime(int num);

int main() {
    int start, end, i;

    printf("Enter two numbers (intervals): ");
    scanf("%d %d", &start, &end);

    printf("Prime numbers between %d and %d are: ", start, end);
    for(i = start; i <= end; i++) {
        if(isPrime(i))
            printf("%d ", i);
    }
    
    return 0;
}

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

2.3 Output

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

Example 3: Prime Number Checker Using Recursion

3.1 Explanation

This example demonstrates a prime number checker using a recursive function.

3.2 Program: Recursive Prime Number Checker

C
#include <stdio.h>

int isPrime(int num, int i);

int main() {
    int start, end, i;

    printf("Enter two numbers (intervals): ");
    scanf("%d %d", &start, &end);

    printf("Prime numbers between %d and %d are: ", start, end);
    for(i = start; i <= end; i++) {
        if(isPrime(i, 2))
            printf("%d ", i);
    }
    
    return 0;
}

int isPrime(int num, int i) {
    if (num <= 2)
        return (num == 2) ? 1 : 0;
    if (num % i == 0)
        return 0;
    if (i * i > num)
        return 1;
    return isPrime(num, i + 1);
}

3.3 Output

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

Conclusion

In this article, we explored how to write a C Program to Display Prime Numbers Between Intervals Using Function. We covered three different solutions:

  1. A simple prime number checker.
  2. An optimized prime number checker using the square root method.
  3. A recursive prime number checker.

Each example demonstrates a unique approach to solving the problem, showcasing the flexibility and power of functions in C programming. By understanding and implementing these examples, you will be well-equipped to handle prime number calculations and other similar tasks in your future C programming projects.