Advertisement
informaticage

Creare algoritmi ricorsivi - video

Oct 12th, 2014
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. // f(1) = 2
  5. // f(n) = 2F(n - 1)
  6.  
  7. using namespace std;
  8.  
  9. int fun ( int n ){
  10.     if ( n == 1 )
  11.         return 2;
  12.     else
  13.         return 2 * fun( n - 1 );
  14. }
  15.  
  16. int main()
  17. {
  18.     int num;
  19.     cout << "Inserire num: " << endl;
  20.     do{
  21.         cin >> num;
  22.     }while(num < 2);
  23.    
  24.     cout << "Funzione: " << fun(num) << endl;
  25.     cout << "Press the enter key to continue ...";
  26.     system("pause >>null");
  27.     return EXIT_SUCCESS;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement