Advertisement
MeehoweCK

Untitled

Mar 26th, 2024
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. /*
  2. Polecenie:
  3. Napisz funkcję unsigned int dzielniki(unsigned int n), która zwraca liczbę dzielników naturalnych podanej liczby n
  4. */
  5.  
  6. #include <iostream>
  7.  
  8. unsigned int dzielniki(unsigned int n) {
  9.     if (n == 0) {
  10.         return 0;
  11.     }
  12.     if (n == 1) {
  13.         return 1;
  14.     }
  15.     unsigned int wynik{2};
  16.     for (auto i{ 2 }; i < n; ++i) {
  17.         if (n % i == 0) {
  18.             ++wynik;
  19.         }
  20.     }
  21.     return wynik;
  22. }
  23.  
  24. bool czyPierwsza(unsigned int liczba) {
  25.     if (dzielniki(liczba) == 2) {
  26.         return true;
  27.     }
  28.     return false;
  29. }
  30.  
  31. int main() {
  32.     unsigned int liczba;
  33.     std::cout << "Podaj liczbe naturalna: ";
  34.     std::cin >> liczba;
  35.     std::cout << liczba << " posiada " << dzielniki(liczba) << " dzielniki naturalne, wiec ";
  36.     if (!czyPierwsza(liczba)) {
  37.         std::cout << "nie ";
  38.     }
  39.     std::cout << "jest liczba pierwsza.\n";
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement