Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created September 27, 2019 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/0a4d5ed99b97d31f2ca784aacec78e8e to your computer and use it in GitHub Desktop.
Save parzibyte/0a4d5ed99b97d31f2ca784aacec78e8e to your computer and use it in GitHub Desktop.
/*
Número primo en C++ o CPP
https://parzibyte.me/blog
*/
#include <iostream>
// Definir prototipo de función
bool esPrimo(int numero);
int main() {
int numero;
std::cout << "Escribe un número y te diré si es primo:\n";
std::cin >> numero;
if (esPrimo(numero)) {
std::cout << "Es primo";
} else {
std::cout << "NO es primo";
}
}
bool esPrimo(int numero) {
// Casos especiales
if (numero == 0 || numero == 1 || numero == 4) return false;
for (int x = 2; x < numero / 2; x++) {
if (numero % x == 0) return false;
}
// Si no se pudo dividir por ninguno de los de arriba, sí es primo
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment