In this article, we will write a C Program to find prime numbers in a given range. For example, if user enters the range from 1 to 10, then program should print prime numbers between 1 to 10, which are 2, 3, 5 and 7.
C Program to print prime numbers in a range
In this program, user is asked to enter the start and end range. The program, then prints all the prime numbers in the entered range. The explanation of the program is at the end of the code.
#include <stdio.h>
#include <stdbool.h>
// Function to check if a number is prime
bool isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) return false;
}
return true;
}
// Function to find and print prime numbers in a given range
void findPrimesInRange(int start, int end) {
printf("Prime numbers between %d and %d are: ", start, end);
for (int i = start; i <= end; i++) {
if (isPrime(i)) {
printf("%d ", i);
}
}
printf("\n");
}
int main() {
int start, end;
printf("Enter the start of the range: ");
scanf("%d", &start);
printf("Enter the end of the range: ");
scanf("%d", &end);
findPrimesInRange(start, end);
return 0;
}
Output:
Enter the start of the range: 1
Enter the end of the range: 50
Prime numbers between 1 and 50 are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Explanation of the program:
1. Include Header files:
#include <stdio.h>
#include <stdbool.h>
#include <stdio.h>
: It is required forprintf
andscanf
functions.#include <stdbool.h>
: It is required forbool
type, which we have used as a return type forisPrime(
) function.
2. Prime Number Check Function: It checks whether a given number is prime or not.
bool isPrime(int n) {
// Numbers less than or equal to 1 are not prime
if (n <= 1) return false;
// 2 and 3 are prime numbers
if (n <= 3) return true;
// Multiples of 2 and 3 are not prime
if (n % 2 == 0 || n % 3 == 0) return false;
// Check for factors from 5 to sqrt(n) using 6k ± 1 rule
for (int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0)
return false;
}
// If no factors are found, the number is prime
return true;
}
- It returns
false
for numbers less than or equal to 1 as these are not prime numbers. - It returns
true
for 2 and 3 as these are prime numbers. - It checks whether the number is divisible by 2 or 3, if it is then it returns false as multiples of 2 and 3 are not prime.
- For remaining numbers, it checks using a loop that runs from 5 to square root of n, in a loop of i+=6.
3. Function to Find Primes in a Range: This function prints the prime numbers in the given range
void findPrimesInRange(int start, int end) {
printf("Prime numbers between %d and %d are: ", start, end);
for (int i = start; i <= end; i++) {
if (isPrime(i)) {
printf("%d ", i); // Print the prime number
}
}
printf("\n");
}
- The
start
andend
variables represents the range. - It runs a loop from
start
toend
. - It checks each number in the range by calling
isPrime()
function. - If
isPrime()
function returns true for a number, it prints the number.
4. Main function:
int main() {
int start, end;
printf("Enter the start of the range: ");
scanf("%d", &start);
printf("Enter the end of the range: ");
scanf("%d", &end);
findPrimesInRange(start, end);
return 0;
}
- It prompts the user to enter the start and end of the range using
printf
and read the user entered values usingscanf
. - It calls
findPrimesInRange
with entered range values to print all prime numbers in entered range. - Finally, it returns 0 to indicate that program executed successfully.
G.jagan says
if it is asked for prime nos betn 0 and 50 …then 1 gets flag o value and gets printed as prime no ..if ur logic is followed …. is it ?