C Program To Find Prime Numbers Between Two Numbers

C Program To Find Prime Numbers Between Two Numbers

The C program to find prime numbers between two given numbers is a versatile and fundamental application of number theory. Prime numbers, being natural numbers greater than 1 with no positive divisors other than 1 and itself, hold significant importance in various mathematical and computational domains. This program employs a systematic approach to identify and display prime numbers within a user-specified range.

By utilizing modular functions and an efficient algorithm, the program offers an insightful example of how C programming can be employed to tackle fundamental mathematical concepts. In this guide, we’ll delve into the algorithm, pseudocode, and implementation details, providing a comprehensive understanding of how the program operates and showcasing its practical application in identifying prime numbers.

How To Find Prime Numbers Between Two Numbers?

Algorithm:

The algorithm to discover prime numbers between two given values is simple. It involves iterating through each number in the given range one by one and figuring out if it is a prime number or not.

Essentially, a prime number is a special kind of number that is greater than 1 and cannot be produced by multiplying two lower numbers together. The program’s ability to recognize and highlight prime numbers inside a given numerical interval is based on this simple method.

1. Begin with the initial number within your specified range.

2. Confirm that the present number exceeds 1 (as 1 doesn’t qualify as a prime).

3. Proceed to examine numbers from 2 up to the square root of the current number.

    a. If the current number divides evenly by any of these in-between numbers,

       it’s not a prime.

    b. Conversely, if there’s no even division, consider it a prime number.

4. Repeat this sequence for every number in the given range.

Pseudocode:

function isPrime(n):

    if n <= 1:

        return False

    for i in range(2, int(sqrt(n)) + 1):

        if n % i == 0:

            return False

    return True

function findPrimesInGivenRange(start, end):

    primes = []

    for num in range(start, end + 1):

        if isPrime(num):

            primes.append(num)

    return primes

C Program To Display Prime Numbers Between Two Intervals

#include <stdio.h>

#include <math.h>

int isPrime(int n) {

    if (n <= 1) {

        return 0;  // Not prime

    }

    for (int i = 2; i <= sqrt(n); i++) {

        if (n % i == 0) {

            return 0;  // Not prime

        }

    }

    return 1;  // Prime

}

void findPrimesInGivenRange(int start, int end) {

    printf("The prime numbers within the given range %d and %d are:\n", start, end);

    for (int num = start; num <= end; num++) {

        if (isPrime(num)) {

            printf("%d ", num);

        }

    }

    printf("\n");

}

int main() {

    int start, end;

    printf("Please Input the range (two numbers separated by space): ");

    scanf("%d %d", &start, &end);

    findPrimesInGivenRange(start, end);

    return 0;

}

Output/Complexity Analysis:

The program’s output will showcase the prime numbers found within the specified range.

Please Input the range (two numbers separated by space): 23 88
The prime numbers within the given range 23 and 88 are:
23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 

In terms of efficiency, the time complexity of this algorithm is expressed as O(n * sqrt(m)), 

where ‘n’ signifies the range of numbers and ‘m’ represents the maximum number within that specified range.

This notation provides an understanding of how the algorithm’s execution time scales with the size of the input, emphasizing its effectiveness in handling different ranges of numerical values.

How Does This Program Work?

The C program for finding prime numbers between two given numbers operates through a systematic process that involves modular functions and iterative checks. Here’s a detailed breakdown of its functioning:

User Input:

The program starts by prompting the user to input a range, consisting of two integer values separated by a space. These two numbers determine the interval within which the program will identify prime numbers.

isPrime Function:

The heart of the program beats in the isPrime function, a vital component crafted to determine whether a given number holds the coveted status of being prime. This function relies on a straightforward mathematical approach. It kicks off by checking if the number is less than or equal to 1, swiftly dismissing it from the prime consideration. Following this initial assessment, the function engages in a loop, ranging from 2 to the square root of the provided number.

With each iteration, the function actively probes for divisibility. If the number divides evenly by any integer in this range, it promptly labels it as non-prime. Conversely, if the loop concludes without uncovering any divisors, it confidently declares the number as prime. This meticulous and active process encapsulates the program’s prowess in decisively discerning the prime or non-prime nature of a given numerical entity.

findPrimesInGivenRange Function:

The findPrimesInGivenRange function utilizes the isPrime function to identify and print prime numbers within the specified range. It calls the isPrime function every time it comes across a number in the given range. It displays on the screen if a number is found to be prime.

Main Function:

In the main function, the program begins by obtaining user input for the range. It then invokes the findPrimesInRange function with the provided range. The resulting prime numbers are displayed on the console, offering a clear output for the user.

By breaking down the program’s operation into these steps, it becomes evident that the effectiveness of this algorithm lies in its ability to systematically assess the primality of each number within the specified range, providing a concise and accurate list of prime numbers as output. Understanding the intricacies of the isPrime function is fundamental to comprehending how the program efficiently identifies prime numbers in a given numerical interval.

Conclusion:

This C program efficiently finds and displays prime numbers between two specified numbers. Understanding the basic concepts of prime numbers and the algorithm used in this program is essential for anyone learning C programming and basic number theory.

Learn C.

Leave a Reply

Your email address will not be published. Required fields are marked *