Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int fattRecur(int n);
  5.  
  6. int main()
  7. {
  8.     int n;
  9.     int fatt;
  10.     cout << "Iserire il numero per il quale si vuole calcolare il fattoriale: ";
  11.     cin >> n;
  12.     fatt = n;
  13.  
  14.     for (int i = fatt - 1; i > 0; i--)
  15.     {
  16.         fatt = fatt * i;
  17.     }
  18.  
  19.     cout << "Il fattoriale di " << n << " equvale a: " << fatt << endl;
  20.     cout << "Il fattoriale calcolato con una funzione recursiva vale: " << fattRecur(n) << endl;
  21.     return 0;
  22. }
  23.  
  24. int fattRecur(int n)
  25. {
  26.     return n == 1 ? 1 : n * fattRecur(n - 1);
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement