fork download
  1. // A C++ program to calculate and print prime numbers from 1 to 50
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. vector<int> findPrimes(int max) {
  8. vector<bool> is_prime(max + 1, true);
  9. vector<int> primes;
  10. for (int i = 2; i <= max; ++i) {
  11. if (is_prime[i]) {
  12. primes.push_back(i);
  13. for (int j = i * 2; j <= max; j += i) {
  14. is_prime[j] = false;
  15. }
  16. }
  17. }
  18. return primes;
  19. }
  20.  
  21. int main() {
  22. vector<int> primes = findPrimes(50);
  23. cout << "Prime numbers from 1 to 50 are:" << endl;
  24. for (int prime : primes) {
  25. cout << prime << " ";
  26. }
  27. cout << endl;
  28. return 0;
  29. }
  30.  
Success #stdin #stdout 0.01s 5268KB
stdin
Standard input is empty
stdout
Prime numbers from 1 to 50 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47