j33vansh

Answer 4 Looping CPP

Jun 27th, 2021 (edited)
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define FIO                           \
  4.     ios_base::sync_with_stdio(false); \
  5.     cin.tie(NULL);                    \
  6.     cout.tie(NULL);
  7.  
  8. int main()
  9. {
  10.     FIO;
  11.     int a, b;
  12.     cin >> a >> b;
  13.     for (int i = a; i <= b; i++) {
  14.         // Skip 0 and 1 as they are
  15.         // neither prime nor composite
  16.         if (i == 1 || i == 0)
  17.             continue;
  18.  
  19.         // flag variable to tell
  20.         // if i is prime or not
  21.         bool flag = 1;
  22.  
  23.         for (int j = 2; j <= i / 2; ++j) {
  24.             if (i % j == 0) {
  25.                 flag = 0;
  26.                 break;
  27.             }
  28.         }
  29.  
  30.         // flag = 1 means i is prime
  31.         // and flag = 0 means i is not prime
  32.         if (flag == 1)
  33.             cout << i << " ";
  34.     }
  35. }
Add Comment
Please, Sign In to add comment