Skip to content

Instantly share code, notes, and snippets.

@juanfal
Created November 4, 2014 07:52
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 juanfal/ac941e75be00a6e3684e to your computer and use it in GitHub Desktop.
Save juanfal/ac941e75be00a6e3684e to your computer and use it in GitHub Desktop.
Comprobación de si un número dado es primo mediante prueba directa, dividiendo por todos los candidatos
// primos.cpp
// juanfc 2014-11-04
// Comprobación de si un número dado es primo mediante
// prueba directa, dividiendo por todos los candidatos
#include <iostream>
using namespace std;
int main()
{
unsigned n;
cout << "Entrar un número natural: ";
cin >> n;
unsigned i = 2;
bool esPrimo = true;
while (i < n and esPrimo) {
esPrimo = n % i != 0;
++i;
}
if (esPrimo) {
cout << "ES primo" << endl;
} else {
cout << "NO es primo" << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment