- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# C++ Program to Display Prime Numbers Between Two Intervals Using Functions

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 Questions & Answers
- C++ Program to Display Prime Numbers Between Two Intervals
- Java Program to Display Prime Numbers Between Two Intervals
- C program to display the prime numbers in between two intervals
- Java Program to Display Prime Numbers Between Intervals Using Function
- Java Program to Display Armstrong Numbers Between Intervals Using Function
- C++ Program to Display Armstrong Number Between Two Intervals
- Java Program to Display Armstrong Number Between Two Intervals
- C program to display all prime numbers between 1 to N using for loop
- Java Program to Display All Prime Numbers from 1 to N
- Program to print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime Using C++
- Program to find Prime Numbers Between given Interval in C++
- Java program to print the Armstrong numbers between two numbers
- C++ Program to Generate Prime Numbers Between a Given Range Using the Sieve of Sundaram
- Find two distinct prime numbers with given product in C++ Program
- How to generate prime numbers using Python?