✓ Link copied to clipboard
C 📅 July 6, 2026 ⏱ 5 min read

C Program to Display Prime Numbers Between Two Intervals

C Program to Display Prime Numbers Between Two Intervals

Need to find prime numbers in a range? This C program lets you check each number in an interval. For example, enter 10 and 20, and it'll find 11, 13, 17, and 19. It's a great way to get how prime checking works.

Prerequisites for the C Program to Display Prime Numbers

Before diving into the code, make sure you're familiar with these concepts:

  • Using <stdio.h> for input and output functions.
  • Understanding loops, particularly the for loop.
  • Basic knowledge of prime numbers and their properties.
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler.

Source Code of C Program to Display Prime Numbers Between Two Intervals

Here's how you can find and display prime numbers between two intervals using C programming:

prime_intervals.cC▶ Run
#include <stdio.h>

int main() {
    int lower, upper, i, j, flag;
    printf("Enter the lower interval: ");
    scanf("%d", &lower);
    printf("Enter the upper interval: ");
    scanf("%d", &upper);

    printf("Prime numbers between %d and %d are: ", lower, upper);

    for (i = lower; i <= upper; ++i) {
        if (i < 2)
            continue;
        flag = 1;
        for (j = 2; j <= i/2; ++j) {
            if (i % j == 0) {
                flag = 0;
                break;
            }
        }
        if (flag)
            printf("%d ", i);
    }

    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Prime Numbers Program

Here's what you can expect when you run the program:

output.txtOutput▶ Run
Enter the lower interval: 10
Enter the upper interval: 20
11 13 17 19
Output of C Program to Display Prime Numbers Between Two Intervals
Output of C Program to Display Prime Numbers Between Two Intervals

Algorithm to Display Prime Numbers in C

The program checks each number in the given range to see if it's prime by trying to divide it by every number less than its square root. Here is the exact logic it uses:

  1. Read the lower and upper bounds of the interval from the user.
  2. For each number in this range, check if it's less than 2. If so, skip it.
  3. Set a flag as true to assume the number might be prime.
  4. Try dividing the number by each integer up to its half to check for factors.
  5. If a factor is found, set the flag to false and break the loop.
  6. If the flag remains true, print the number as it is prime.
  7. Repeat for the entire interval.
flowchart TD
  A([Start]) --> B[/Read lower and upper/]
  B --> C[Check each number in range]
  C --> D{Is number >= 2?}
  D -->|Yes| E{Is prime?}
  E -->|Yes| F[Print number]
  E -->|No| G[Continue]
  G --> H[/Increment number/]
  H --> C
  D -->|No| I([End])
Flowchart: the algorithm step by step

How the C Program to Display Prime Numbers Works

Let's break down how each part of the program functions.

  • #include <stdio.h>: Includes the standard input/output library for using printf and scanf.
  • int main(): The main function where execution begins.
  • Declare variables lower, upper, i, j, and flag for maintaining intervals, loop counters, and prime checks.
  • Prompt and read the lower and upper interval values from the user using scanf.
  • Loop from lower to upper to check each number.
  • Inside the loop, skip numbers less than 2 since they aren't prime.
  • Assume each number is prime by setting flag to true.
  • Use another loop to check for factors up to half the number; if found, set flag to false and break.
  • If no factors are found (i.e., flag is true), print the number as a prime.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What is the main function of the inner loop in this program?

🎉Correct! The inner loop checks each number in the range to determine if it's prime.
🤔Not quite. Think about how the program identifies prime numbers.

Time and Space Complexity of the C Program to Display Prime Numbers

Let's evaluate how efficient this program is.

Time Complexity: O(n√n). Here, n is the count of numbers in the interval. For each number, it checks divisibility up to its half, meaning 10 numbers means about 31 checks.

Space Complexity: O(1). It only uses a few integer variables like lower, upper, i, j, and flag.

⚠️

Common mistake: Not resetting the flag variable for each number can cause wrong results.

Key takeaway: Check numbers up to their square root for better prime verification.

FAQs About Displaying Prime Numbers in C

Here are some common queries about finding prime numbers using C:

Numbers less than 2 aren't prime by definition.
The program doesn't handle this case, so you'll need to tweak it yourself.
Yep, but it ignores negative numbers since they aren't prime.
Checking up to half the number is enough and way more efficient.
The flag's there to see if a number is prime after the checks.

If you found this program helpful, check out these related programs:

Conclusion

When checking primes, it's key to only test necessary factors. Next, try writing a program for the greatest common divisor of two numbers. You'll notice similar logic at play. Brush up on your C skills with related quizzes and the compiler.

Test more C programs yourself. Take the C programming quiz or run code in the C online compiler.