Monday, 6 February 2017

Printing Prime numbers between a given range..


#include<iostream>
using namespace std;
void main()
{
 int lb;
 int ub;
 int sum = 0;
 cout << "Enter the lower bound of the range " << endl;
 cin >> lb;
 cout << "Enter the upper bound of the range " << endl;
 cin >> ub;
 for (int i = lb; i <= ub; ++i) {
  sum = 0;
  for (int j = 1; j <= i; ++j) {
   if (i%j == 0) {
    sum++;
   }
  }
  if (sum == 2) {
   cout << i << " , ";
  }
 }
 cout << endl;
 system("pause");
}

No comments:

Post a Comment