askvity

How to Print Prime Numbers Within a Range in C?

Published in C Programming Prime Numbers 2 mins read

To print prime numbers in C, particularly within a range specified by the user, you typically follow a process that involves iterating through the numbers in the range and checking each one for primality.

Understanding the Process

The core idea is to check every number within a given start and end value to see if it meets the definition of a prime number (a natural number greater than 1 that has no positive divisors other than 1 and itself).

Based on the provided reference (18-Apr-2020), the steps involved are:

  1. User Input: Obtain the desired range (e.g., start and end numbers) from the user.
  2. Iteration: Use a loop (like a for loop) to go through each number sequentially within the specified range.
  3. Primality Check: For each number in the loop, pass it to a function (often named isprime or similar) designed to check if it is a prime number. This function returns a boolean value (TRUE or FALSE, or 1 or 0 in C).
  4. Printing: If the isprime function returns TRUE (meaning the number is prime), the program prints the number.
  5. Skipping: If the isprime function returns FALSE, the program skips the number and moves to the next one in the range. The continue statement in C can be used for this purpose, although simply not printing is usually sufficient and the default behavior if the condition isn't met.

Detailed Steps

Let's break down the typical implementation:

  • Getting the Range: You would use C's input functions (scanf) to ask the user for the lower and upper bounds of the range they are interested in.
  • Looping Through Numbers: A for loop is ideal for this. It starts from the lower bound and continues up to the upper bound, incrementing by 1 in each iteration.
    for (int number = start_range; number <= end_range; number++) {
        // Check if 'number' is prime
    }
  • Implementing the isprime Function: This function is crucial. It takes an integer as input and determines if it's prime.
    • Numbers less than or equal to 1 are not prime, so return FALSE.
    • The number 2 is prime, return TRUE.
    • For numbers greater than 2, you check for divisibility by numbers from 2 up to the square root of the number. If you find any number that divides it evenly, it's not prime, so return FALSE.
    • If the loop completes without finding any divisors, the number is prime, return TRUE.

Example Structure (Conceptual C Code)

#include <stdio.h>
#include <stdbool.h> // For true/false
#include <math.h>    // For sqrt

// Function to check if a number is prime
bool isprime(int num) {
    if (num <= 1) return false;
    if (num == 2) return true;
    if (num % 2 == 0) return false; // Skip even numbers > 2

    // Check divisibility from 3 up to sqrt(num) in steps of 2
    for (int i = 3; i * i <= num; i = i + 2) {
        if (num % i == 0) return false; // Found a divisor, not prime
    }
    return true; // No divisors found, it's prime
}

int main() {
    int start_range, end_range;

    // 1. User inputs the range.
    printf("Enter the start of the range: ");
    scanf("%d", &start_range);
    printf("Enter the end of the range: ");
    scanf("%d", &end_range);

    printf("Prime numbers between %d and %d are:\n", start_range, end_range);

    // 2. Using the for loop, each number in the range is sent to the isprime function.
    for (int number = start_range; number <= end_range; number++) {
        if (isprime(number)) { // Check if the function returns TRUE
            // 3. if TRUE : program prints the number.
            printf("%d ", number);
        } else {
            // 4. if FALSE : program skips the number (using continue or simply by structure).
            // The 'continue' keyword would go here, but is redundant if only printing on TRUE
            // continue;
        }
    }
    printf("\n"); // Newline at the end
    return 0;
}

Summary Table

Here's a breakdown of the steps used to print prime numbers in C based on the described method:

Step Description C Implementation Concept
Get Range User specifies the start and end numbers for the check. Use scanf to read inputs.
Iterate Loop through every integer from the start to the end of the range. Employ a for loop.
Check Primality Determine if the current number in the loop is prime. Call a separate isprime function.
Print (if prime) If isprime returns TRUE, display the number. Use printf inside the loop if isprime is true.
Skip (if not) If isprime returns FALSE, move to the next number in the range. The else block (or lack of print) handles this. continue is optional.

By following these steps, you can effectively find and print prime numbers within any given range using the C programming language.

Related Articles