Advertisement
Abelsor

VPL - Ejercicio 3

Mar 9th, 2022
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. /*
  2.                         VPL - Ejercicio 3
  3.     Escribir un programa que reciba un número entero, el cual es el indice de un menu de 2 opciones
  4.     Opcion 1: Ingrese un numero 'n' y calcula el factorial de 'n'
  5.     Opcion 2: Ingrese un numero 'n' y un numero 'm', Calcular combinacion(n,m)
  6. */
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. int combinaciones(int, int);
  11. long long factorial(int);//usar long long para no desborde tan rapido
  12.  
  13. int aux;
  14.  
  15. int main(){
  16.   int opcion,n,m;
  17.  
  18.     cout<<"Opcion: ";
  19.     cin>>opcion;
  20.    
  21.     if(opcion==1){
  22.         cout<<"Calcular factorial de: ";
  23.         cin >>n;
  24.         cout<<factorial(n);
  25.     }
  26.     else if(opcion==2){
  27.         cout<<"Numero a combinar: ";
  28.         cin >>n;
  29.         cout<<"De a cuanto desea combinar: ";
  30.         cin>>m;
  31.        
  32.         cout<<combinaciones(n,m)<<endl;
  33.         }
  34.     return 0;
  35. }
  36.  
  37. int combinaciones(int n, int m){
  38.     int combinacion = (factorial(n))/(factorial(m)*factorial(n-m));
  39.    
  40.     return combinacion;
  41. }
  42.  
  43. long long factorial(int n){
  44.     long long factorial=1;
  45.    
  46.     for(int i=1 ; i<=n ; i++){
  47.         factorial *= i;
  48.     }
  49.    
  50.     return factorial;
  51.    
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement