Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream.h>
- // FACTORIAL RECURSIVO
- int f_recursivo ( int n )
- {
- if( n<0 ) return 0;
- if( n<=1 ) return 1;
- return n*f_recursivo(n-1);
- }
- // FACTORIAL ITERATIVO
- int f_iterativo ( int n )
- {
- int i;
- if( n<0 ) return 0;
- if( n==0 ) return 1;
- for( i=n-1 ; i>=2 ; i-- ) n*=i;
- return n;
- }
- int main()
- {
- int n, menu;
- do
- {
- system("cls");
- cout << " 1. Recursivo \n 2. Iterativo \n 0. Salir ";
- cin >> menu;
- }while( menu<0 || menu>2 );
- if( menu==0 ) return 0;
- cout << "Factorial de: ";
- cin >> n;
- if( menu==1 ) n=f_recursivo(n);
- if( menu==2 ) n=f_iterativo(n);
- if( n==0 ) cout << "Nada de negativos" << endl;
- else cout << n << endl;
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement