
- 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
C++ Program to Display Prime Numbers Between Two Intervals
A prime number is a whole number that is greater than one and the only factors of a prime number should be one and itself. Some of the first prime numbers are 2, 3, 5, 7, 11, 13 ,17 etc.
There can be many prime numbers between two intervals. For example, the prime numbers between the intervals 5 and 20 are −
5, 7, 11, 13, 17 and 19.
The program to find and display the prime numbers between two intervals is given as follows.
Example
#include <iostream> using namespace std; void PrimeNumbers (int lbound, int ubound) { int flag, i; while (lbound <= ubound) { flag = 0; for(i = 2; i <= lbound/2; i++) { if(lbound % i == 0) { flag = 1; break; } } if (flag == 0) cout<<lbound<<" "; lbound++; } } int main() { int lowerbound = 20, upperbound = 50; cout<<"Prime numbers between "<<lowerbound<<" and "<<upperbound<<" are: "; PrimeNumbers(lowerbound,upperbound); return 0; }
Output
Prime numbers between 20 and 50 are: 23 29 31 37 41 43 47
In the above program, the function main() contains only the cout object and the function call to the function PrimeNumbers() with upperbound and lowerbound as arguments. This can be seen in the following code snippet.
cout<<"Prime numbers between "<<lowerbound<<" and "<<upperbound<<" are: "; PrimeNumbers(lowerbound,upperbound);
In the function PrimeNumbers(), each number from lbound to ubound is tested to see if it is prime or not. If it is a prime number, it is displayed. This is done using a while loop.
In the while loop, initial value of flag=0. If the number is not prime, then the value of flag is set to 1 in the for loop. After the end of the for loop, if flag is still 0, then the number is prime and it is displayed. This can be observed from the following code snippet.
while (lbound <= ubound) { flag = 0; for(i = 2; i <= lbound/2; i++) { if(lbound % i == 0) { flag = 1; break; } } if (flag == 0) cout<<lbound<<" "; lbound++; }
- Related Articles
- Java Program to Display Prime Numbers Between Two Intervals
- Swift program to display prime numbers between two intervals
- Haskell Program to Display Prime Numbers Between Two Intervals
- C++ Program to Display Prime Numbers Between Two Intervals Using Functions
- C program to display the prime numbers in between two intervals
- Golang Program to Display Prime Numbers Between Two Intervals using library functions.
- Java Program to Display Prime Numbers Between Intervals Using Function
- Swift program to display prime numbers between intervals using function
- Haskell Program to Display Prime Numbers Between Intervals Using Function
- C++ Program to Display Armstrong Number Between Two Intervals
- Java Program to Display Armstrong Number Between Two Intervals
- Swift Program to Display Armstrong Number Between Two Intervals
- Golang Program to Display Armstrong Number Between Two Intervals
- Haskell Program to Display Armstrong Number Between Two Intervals
- Java Program to Display Armstrong Numbers Between Intervals Using Function
