Program to find Prime Numbers Between given Interval

Given two numbers
and
as interval range, the task is to find the prime numbers in between this interval.
Examples:
Input : a = 1, b = 10 Output : 2, 3, 5, 7 Input : a = 10, b = 20 Output : 11, 13, 17, 19
In the below program, the range of numbers is taken as input and stored in the variables ‘a’ and ‘b’. Then using for-loop, the numbers between the interval of a and b are traversed. For each number in the for loop, it is checked if this number is prime or not. If found prime, print the number. Then the next number in the loop is checked, till all numbers are checked.
Program:
C++
// C++ program to find the prime numbers // between a given interval #include <bits/stdc++.h> using namespace std; int main() { // Declare the variables int a, b, i, j, flag; // Ask user to enter lower value of interval cout << "Enter lower bound of the interval: "; cin >> a; // Take input // Ask user to enter upper value of interval cout << "\nEnter upper bound of the interval: "; cin >> b; // Take input // Print display message cout << "\nPrime numbers between " << a << " and " << b << " are: "; // Traverse each number in the interval // with the help of for loop for (i = a; i <= b; 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; 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) cout << i << " "; } return 0; } // This code is contributed by Akanksha Rai |
C
// C program to find the prime numbers // between a given interval #include <stdio.h> int main() { // Declare the variables int a, b, i, j, flag; // Ask user to enter lower value of interval printf("Enter lower bound of the interval: "); scanf("%d", &a;); // Take input // Ask user to enter upper value of interval printf("\nEnter upper bound of the interval: "); scanf("%d", &b;); // Take input // Print display message printf("\nPrime numbers between %d and %d are: ", a, b); // Traverse each number in the interval // with the help of for loop for (i = a; i <= b; 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; 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); } return 0; } |
Java
import java.util.Scanner; // Java program to find the prime numbers // between a given interval public class GFG { // driver code public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Declare the variables int a, b, i, j, flag; // Ask user to enter lower value of interval System.out.printf("Enter lower bound of the interval: "); a = sc.nextInt(); // Take input // Ask user to enter upper value of interval System.out.printf("\nEnter upper bound of the interval: "); b = sc.nextInt(); // Take input // Print display message System.out.printf("\nPrime numbers between %d and %d are: ", a, b); // Traverse each number in the interval // with the help of for loop for (i = a; i <= b; 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; 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) { System.out.println(i); } } } |
Python3
# Python3 program to find the prime # numbers between a given interval if __name__ == '__main__': # Declare the variables a, b, i, j, flag = 0, 0, 0, 0, 0 # Ask user to enter lower value of interval print("Enter lower bound of the interval:", end = "") a = int(input()) # Take input print(a) # Ask user to enter upper value of interval print("Enter upper bound of the interval:", end = "") b = int(input()) # Take input print(b) # Print display message print("Prime numbers between", a, "and", b, "are:", end = "") # Traverse each number in the interval # with the help of for loop for i in range(a, b + 1): # Skip 1 as1 is niether # prime nor composite if (i == 1): continue # flag variable to tell # if i is prime or not flag = 1 for j in range(2, i // 2 + 1): if (i % j == 0): flag = 0 break # flag = 1 means i is prime # and flag = 0 means i is not prime if (flag == 1): print(i, end = " ") # This code is contributed # by Mohit kumar 29 |
Output:
Enter lower bound of the interval: 1 Enter upper bound of the interval: 10 Prime numbers between 1 and 10 are: 2 3 5 7
Recommended Posts:
- Python program to print all Prime numbers in an Interval
- Program to find sum of prime numbers between 1 to n
- Find two prime numbers with given sum
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Find product of prime numbers between 1 to n
- Find count of Almost Prime numbers from 1 to N
- Find the Product of first N Prime Numbers
- Find two distinct prime numbers with given product
- Find the prime numbers which can written as sum of most consecutive primes
- Find a sequence of N prime numbers whose sum is a composite number
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Find the highest occurring digit in prime numbers in a range
- Program to find the Nth Prime Number
- C Program for Find largest prime factor of a number
- C/C++ Program to find Product of unique prime factors of a number
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.



