
- 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
Print prime numbers in a given range using C++ STL
It is the program to print prime numbers in a given range.
Algorithms
Begin Declare a user define datatype stl. Declare a vector number to the stl datatype. Declare variable a to the stl datatype. Declare vector Prime_Number to the Boolean datatype. Prime_Number[0] = false. Prime_Number[1] = false. Declare b to the integer datatype. Initialize b = sqrt(a). for (stl pr=2; pr<=b; pr++) if (Prime_Number[pr]) then for (stl i=pr*2; i<=a; i += pr) Prime_Number[i] = false. Declare result to the vector type. for (int i=0;i<a;i++) if (Prime_Number[i]) then result.push_back(i). return result. End Begin Declare function remove_zero() to the Boolean type. Declare i to the stl datatype. Return i = 0. End Begin Declare a vector Number_Range to the stl datatype. Declare First_Num, Last_Num to the stl datatype. Pass them as parameter. Declare two vector s1 and s2 to the stl datatype. Initialize s1 = number(First_Num) to find primes from o to First_Num. Initialize s2 = number(Last_Num) to find primes from o to Last_Num. Declare vector result(Last_Num-First_Num) to the stl datatype. Call set_difference(s2.begin(), s2.end(), s1.begin(), s2.end(), result.begin()) to find set difference of two vectors. Declare an iterator itr to the vector type. Initialize itr = remove_if(result.begin(),result.end(),remove_zero) to remove extra zeros. Call resize (itr-result.begin()) to resize the result. Return result. Declare First_Num, Last_Num to the stl datatype. Initialize First_Num = 20, Last_Num = 50. Declare a vector result to the stl datatype. Initialize result = Number_Range(First_Num,Last_Num). Print "The Prime Numbers from 20 to 50 are: “ for (auto i:result) Print the values of i. End.
Example
#include<bits/stdc++.h> using namespace std; typedef unsigned long long int stl; vector<stl> number(stl a) { vector<bool> Prime_Number(a+1,true); Prime_Number[0] = false; Prime_Number[1] = false; int b = sqrt(a); for (stl pr=2; pr<=b; pr++) { if (Prime_Number[pr]) { for (stl i=pr*2; i<=a; i += pr) Prime_Number[i] = false; } } vector<stl> result; for (int i=0;i<a;i++) if (Prime_Number[i]) result.push_back(i); return result; } bool remove_zero(stl i) { // Used to remove zeros from a vector return i == 0; } vector<stl> Number_Range(stl First_Num,stl Last_Num) { vector<stl> s1 = number(First_Num); // find primes from o to First_Num vector<stl> s2 = number(Last_Num); // find primes from o to Last_Num vector<stl> result(Last_Num-First_Num); set_difference(s2.begin(), s2.end(), s1.begin(), s2.end(), result.begin()); // find set //difference of two vectors vector<stl>::iterator itr = remove_if(result.begin(),result.end(),remove_zero); //remove extra zeros. result.resize(itr-result.begin()); return result; } int main(void) { stl First_Num = 20, Last_Num = 50; vector<stl> result = Number_Range(First_Num,Last_Num); cout<<"The Prime Numbers from "<<First_Num<<" to "<<Last_Num<< " are: "; for (auto i:result) cout<<i<<' '; return 0; }
Output
The Prime Numbers from 20 to 50 are: 23 29 31 37 41 43 47
- Related Articles
- Program to print prime numbers in a given range using C++ STL
- Prime numbers in a range - JavaScript
- Write a Golang program to find prime numbers in a given range
- Print all Good numbers in given range in C++
- Python - Find the number of prime numbers within a given range of numbers
- C++ Program to Generate Prime Numbers Between a Given Range Using the Sieve of Sundaram
- Golang Program to Print Odd Numbers Within a Given Range
- Prime numbers within a range in JavaScript
- 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
- Counting prime numbers that reduce to 1 within a range using JavaScript
- Sum of prime numbers between a range - JavaScript
- How to print array elements within a given range using Numpy?
- How to Print all Prime Numbers in an Interval using Python?
- How to print the range of numbers using C language?

Advertisements