Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Program to find Prime Numbers Between given Interval in C++
In this tutorial, we will be discussing a program to find prime numbers between given intervals.
For this we would be provided with two integers. Our task is to find the prime numbers in that particular range.
Example
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, i, j, flag;
//getting lower range
a = 3;
//getting upper range
b = 12;
cout << "\nPrime numbers between "
<< a << " and " << b << " are: ";
for (i = a; i <= b; i++) {
if (i == 1 || i == 0)
continue;
flag = 1;
for (j = 2; j <= i / 2; ++j) {
if (i % j == 0) {
flag = 0;
break;
}
}
if (flag == 1)
cout << i << " ";
}
return 0;
}
Output
Prime numbers between 3 and 12 are: 3 5 7 11
Advertisements