#include <iostream>
using namespace std;
bool esPrimo(int num) {
if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 == 0 || num % 3 == 0) return false;
for (int i = 5; i * i <= num; i += 6) {
if (num % i == 0 || num % (i + 2) == 0) return false;
}
return true;
}
int main() {
int N;
cout << "Ingresa un número entero positivo N: ";
cin >> N;
if (N <= 0) {
cout << "Por favor, ingresa un número entero positivo." << endl;
return 1;
}
int contador_primos = 0;
int num = 2;
while (num <= N) {
if (esPrimo(num)) {
contador_primos++;
}
num++;
}
cout << "La cantidad de números primos entre 1 y " << N << " es: " << contador_primos << endl;
return 0;
}
To embed this program on your website, copy the following code and paste it into your website's HTML: