Advertisement
Kyrexar

Factorial

Feb 7th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream.h>
  2.  
  3. // FACTORIAL RECURSIVO
  4. int f_recursivo ( int n )
  5. {
  6.     if( n<0 ) return 0;
  7.     if( n<=1 ) return 1;
  8.     return n*f_recursivo(n-1);
  9. }
  10.  
  11. // FACTORIAL ITERATIVO
  12. int f_iterativo ( int n )
  13. {
  14.     int i;
  15.    
  16.     if( n<0 ) return 0;
  17.     if( n==0 ) return 1;
  18.     for( i=n-1 ; i>=2 ; i-- ) n*=i;
  19.     return n;
  20.  
  21. }
  22.  
  23. int main()
  24. {
  25.     int n, menu;
  26.    
  27.     do
  28.     {
  29.         system("cls");
  30.         cout << " 1. Recursivo \n 2. Iterativo \n 0. Salir ";
  31.         cin >> menu;
  32.     }while( menu<0 || menu>2 );
  33.    
  34.     if( menu==0 ) return 0;
  35.    
  36.     cout << "Factorial de: ";
  37.     cin >> n;
  38.        
  39.     if( menu==1 ) n=f_recursivo(n);
  40.     if( menu==2 ) n=f_iterativo(n);
  41.    
  42.     if( n==0 ) cout << "Nada de negativos" << endl;
  43.     else cout << n << endl;
  44.  
  45.     system("PAUSE");
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement