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
forloop. - 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:
#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.
Output of the Prime Numbers Program
Here's what you can expect when you run the program:
Enter the lower interval: 10
Enter the upper interval: 20
11 13 17 19

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:
- Read the lower and upper bounds of the interval from the user.
- For each number in this range, check if it's less than 2. If so, skip it.
- Set a flag as true to assume the number might be prime.
- Try dividing the number by each integer up to its half to check for factors.
- If a factor is found, set the flag to false and break the loop.
- If the flag remains true, print the number as it is prime.
- 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])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 usingprintfandscanf.int main(): The main function where execution begins.- Declare variables
lower,upper,i,j, andflagfor maintaining intervals, loop counters, and prime checks. - Prompt and read the lower and upper interval values from the user using
scanf. - Loop from
lowertoupperto check each number. - Inside the loop, skip numbers less than 2 since they aren't prime.
- Assume each number is prime by setting
flagto true. - Use another loop to check for factors up to half the number; if found, set
flagto false and break. - If no factors are found (i.e.,
flagis true), print the number as a prime.
Quick Quiz — test your understanding
Tap an option to check your answerWhat is the main function of the inner loop in this program?
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:
Related Programs
If you found this program helpful, check out these related programs:
- C Program to Check Armstrong Number
- C Program to Display Armstrong Number Between Two Intervals
- C Program to Display Factors of a Number
- C Program to Check Prime or Armstrong Number Using User-defined Function
- C Program to Check Whether a Number is Prime or Not
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.