
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to print prime numbers in a given range using C++ STL
In this tutorial, we will be discussing a program to print prime numbers for a given range of numbers using the C++ Standard Template Library.
In this, we will be given two numbers say a and b. The task is to print all the coming prime numbers in this range. For this, we will be using the Sieve of Eratosthenes method by running it as a subroutine. Simultaneously we will be storing all the prime numbers in a vector and finally printing them all.
Example
#include<bits/stdc++.h> using namespace std; typedef unsigned long long int unll; vector<unll> eratosthemes(unll n){ vector<bool> prime_num(n+1,true); prime_num[0] = false; prime_num[1] = false; int m = sqrt(n); for (unll p=2; p<=m; p++){ if (prime_num[p]){ for (unll i=p*2; i<=n; i += p) prime_num[i] = false; } } vector<unll< elements; for (int i=0;i<n;i++) if (prime_num[i]) elements.push_back(i); return elements; } bool check_zero(unll i){ return i == 0; } vector<unll> sieve_range(unll start,unll end){ vector<unll> s1 = eratosthemes(start); vector<unll> s2 = eratosthemes(end); vector<unll> elements(end-start); set_difference(s2.begin(), s2.end(), s1.begin(), s2.end(), elements.begin()); vector<unll>::iterator itr = remove_if(elements.begin(),elements.end(),check_zero); elements.resize(itr-elements.begin()); return elements; } int main(void){ unll start = 10; unll end = 90; vector<unll> elements = sieve_range(start,end); for (auto i:elements) cout<<i<<' '; return 0; }
Output
11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89
- Related Articles
- Print prime numbers in a given range using C++ STL
- Write a Golang program to find prime numbers in a given range
- Golang Program to Print Odd Numbers Within a Given Range
- C++ Program to Generate Prime Numbers Between a Given Range Using the Sieve of Sundaram
- Python Program to Print all Numbers in a Range Divisible by a Given Number
- Golang Program to Print all Numbers in a Range Divisible by a Given Number
- C++ Program to Implement Wheel Sieve to Generate Prime Numbers Between Given Range
- C++ Program to Implement Segmented Sieve to Generate Prime Numbers Between Given Range
- Python program to print all even numbers in a range
- Python program to print all odd numbers in a range
- C++ Program to Implement Sieve of eratosthenes to Generate Prime Numbers Between Given Range
- C++ Program to Implement Sieve of Atkin to Generate Prime Numbers Between Given Range
- Python Program to Print Numbers in a Range (1,upper) Without Using any Loops
- Prime numbers in a range - JavaScript
- Java program to print prime numbers below 100

Advertisements