BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to find prime numbers in a given range

Last Updated: May 20, 2024 by Chaitanya Singh | Filed Under: C Programs

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 for printf and scanf functions.
  • #include <stdbool.h>: It is required for bool type, which we have used as a return type for isPrime() 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 and end variables represents the range.
  • It runs a loop from start to end.
  • 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 using scanf.
  • 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.

Related C Programs:

  • C Program to print prime numbers from 1 to 100 (or 1 to N)
  • C Program to check whether a number is prime or not
  • Java Program to check prime number
  • Python Program to check if a number is prime or not
❮ C TutorialC Programs ❯

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. G.jagan says

    August 30, 2015 at 10:57 AM

    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 ?

    Reply

Leave a Reply Cancel reply

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

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap