Given two numbers **L** and **R, **the task is to find the prime numbers between **L** and **R**.

**Examples:**

Input:L = 1, R = 10Output:2 3 5 7

Explanation:

Prime number between the 1 and 10 are 2, 3, 5, and 7

Input:L = 30, R = 40Output:31 37

**Approach:** The idea is to iterate from in the range **[L, R]** and check if any number in the given range is prime or not. If yes then print that number and check for the next number till we iterate all the numbers.

Below the implementation of the above approach:

## C

`// C program to find the prime numbers ` `// between a given interval ` `#include <stdio.h> ` ` ` `// Function for print prime ` `// number in given range ` `void` `primeInRange(` `int` `L, ` `int` `R) ` `{ ` ` ` `int` `i, j, flag; ` ` ` ` ` `// Traverse each number in the ` ` ` `// interval with the help of for loop ` ` ` `for` `(i = L; i <= R; i++) { ` ` ` ` ` `// Skip 0 and 1 as they are ` ` ` `// niether prime nor composite ` ` ` `if` `(i == 1 || i == 0) ` ` ` `continue` `; ` ` ` ` ` `// flag variable to tell ` ` ` `// if i is prime or not ` ` ` `flag = 1; ` ` ` ` ` `// Iterate to check if i is prime ` ` ` `// or not ` ` ` `for` `(j = 2; j <= i / 2; ++j) { ` ` ` `if` `(i % j == 0) { ` ` ` `flag = 0; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// flag = 1 means i is prime ` ` ` `// and flag = 0 means i is not prime ` ` ` `if` `(flag == 1) ` ` ` `printf` `(` `"%d "` `, i); ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given Range ` ` ` `int` `L = 1; ` ` ` `int` `R = 10; ` ` ` ` ` `// Function Call ` ` ` `primeInRange(L, R); ` ` ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

## C++

`// C++ program to find the prime numbers ` `// between a given interval ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function for print prime ` `// number in given range ` `void` `primeInRange(` `int` `L, ` `int` `R) ` `{ ` ` ` `int` `flag; ` ` ` ` ` `// Traverse each number in the ` ` ` `// interval with the help of for loop ` ` ` `for` `(` `int` `i = L; i <= R; i++) { ` ` ` ` ` `// Skip 0 and 1 as they are ` ` ` `// niether prime nor composite ` ` ` `if` `(i == 1 || i == 0) ` ` ` `continue` `; ` ` ` ` ` `// flag variable to tell ` ` ` `// if i is prime or not ` ` ` `flag = 1; ` ` ` ` ` `// Iterate to check if i is prime ` ` ` `// or not ` ` ` `for` `(` `int` `j = 2; j <= i / 2; ++j) { ` ` ` `if` `(i % j == 0) { ` ` ` `flag = 0; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// flag = 1 means i is prime ` ` ` `// and flag = 0 means i is not prime ` ` ` `if` `(flag == 1) ` ` ` `cout << i << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given Range ` ` ` `int` `L = 1; ` ` ` `int` `R = 10; ` ` ` ` ` `// Function Call ` ` ` `primeInRange(L, R); ` ` ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

**Output:**

2 3 5 7

**Time Complexity:** *O((R-L)*N)*, where N is the number, and L and R are the given range.**Auxiliary Space:** *O(1)*

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count all prime numbers in a given range whose sum of digits is also prime
- Count numbers in a given range whose count of prime factors is a Prime Number
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
- Queries for the difference between the count of composite and prime numbers in a given range
- Count pairs from a given range whose sum is a Prime Number in that range
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Program to find Prime Numbers Between given Interval
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Find the highest occurring digit in prime numbers in a range
- Program to find sum of prime numbers between 1 to n
- Print prime numbers in a given range using C++ STL
- Prime numbers in a given range using STL | Set 2
- Sum of all the prime numbers in a given range
- Count of Double Prime numbers in a given range L to R

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.