Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath> // pentru sqrt() -> square root -> radical
- using namespace std;
- int main() {
- int n;
- cout << "n = ";
- cin >> n;
- // check if a number is prime and print it
- for (int i = 2; i <= n; i++) {
- bool isPrime = true; // assume the number is prime
- for (int j = 2; j <= sqrt(i); j++) {
- if (i % j == 0) { // if the number is divisible by j then it is not prime
- isPrime = false; // so we change the value of isPrime to false
- break; // and we stop the loop
- }
- }
- if (isPrime) { // if the number is prime then we print it
- cout << i << " ";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment