MeehoweCK

Untitled

Sep 10th, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. unsigned long long silnia(int n)
  6. {
  7.     if(n == 0)
  8.         return 1;
  9.     return n * silnia(n-1);
  10. }
  11.  
  12. int pobierz_liczbe()
  13. {
  14.     int liczba;
  15.     cin >> liczba;
  16.  
  17.     while(cin.fail() || liczba < 0)       // dopóki przy pobraniu liczby występuje błąd
  18.     {
  19.         cout << "Podana liczba jest nieprawidlowa, wpisz ja jeszcze raz: ";
  20.         cin.clear();
  21.         cin.sync();
  22.         cin >> liczba;
  23.     }
  24.  
  25.     return liczba;
  26. }
  27.  
  28. int main()
  29. {
  30.     int n;
  31.  
  32.     cout << "Podaj liczbe naturalna: ";
  33.     n = pobierz_liczbe();
  34.  
  35.     do
  36.     {
  37.         cout << n << "! = " << silnia(n) << endl
  38.              << "Wpisz 0, aby zakoczyc dzialanie programu lub podaj nowa liczbe naturalna: ";
  39.         n = pobierz_liczbe();
  40.     }
  41.     while(n != 0);
  42.  
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment