Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- bool estePrim(int n) {
- if (n <= 1) {
- return false; // 0 si 1 nu sunt numere prime
- }
- for (int i = 2; i <= sqrt(n); i++) {
- if (n % i == 0) {
- return false; // Daca n este divizibil cu i, atunci n nu este prim
- }
- }
- return true; // Daca a trecut de toate testele, este prim
- }
- int main() {
- int n;
- cout << "n = ";
- cin >> n;
- if (estePrim(n) == true) {
- cout << "Numarul " << n << " este prim" << endl;
- }
- else {
- cout << "Numarul " << n << " nu este prim" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment