Skip to content

Instantly share code, notes, and snippets.

@modos
Created October 31, 2020 10:07
Show Gist options
  • Save modos/71e89aa8bb93834f97b8b15e925306e2 to your computer and use it in GitHub Desktop.
Save modos/71e89aa8bb93834f97b8b15e925306e2 to your computer and use it in GitHub Desktop.
اول بینی
// C++ program to find the prime numbers
// between a given interval
#include <bits/stdc++.h>
using namespace std;
// Function for print prime
// number in given range
void primeInRange(int L, int R)
{
int flag;
bool comma = false;
// Traverse each number in the
// interval with the help of for loop
for (int i = L; i <= R; i++) {
// Skip 0 and 1 as they are
// niether prime nor composite
if (i == 1 || i == 0)
continue;
// flag variable to tell
// if i is prime or not
flag = 1;
// Iterate to check if i is prime
// or not
for (int j = 2; j <= i / 2; ++j) {
if (i % j == 0) {
flag = 0;
break;
}
}
// flag = 1 means i is prime
// and flag = 0 means i is not prime
if (flag == 1)
if (comma){
cout << "," << i;
}else {
cout << i;
comma = true;
}
}
}
// Driver Code
int main()
{
// Given Range
int L;
int R;
cin >> L;
cin >> R;
// Function Call
primeInRange(++L, --R);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment