Program to find Prime Numbers Between given Interval in C++

C++Server Side ProgrammingProgramming

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

 Live Demo

#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
raja
Published on 19-May-2020 15:12:43
Advertisements