Professional Documents
Culture Documents
Taller 8 - Recursividad
Taller 8 - Recursividad
//Friendly numbers
int sumDivisors(int n, int div){
if (div == 1){
return 1;
}
if (n % div == 0){
return div + sumDivisors(n, div - 1);
}
return sumDivisors(n, div - 1);
}
//Write vertically
void printVertically(int n){
if (n < 10) {
cout << n << endl;
}
else{
printVertically(n / 10);
cout << n % 10 << endl;
}
}
//Choice
int choice(int n, int m){
if (m == 0 || m == n){
return 1;
}
return fact(n) / (fact(m) * fact(n - m));
}
int main() {
int option, n, m;
while(option != -1){
cout << "\nChoose an option" << endl;
cout << "\nOption 1: friends numbers" << endl;
cout << "Option 2: write vertically" << endl;
cout << "Option 3: choice" << endl;
cout << "\nInsert -1 if you want to exit the code" << endl;
cout << "\nOption: ";
cin >> option;
switch(option){
case 1:
cout << "\nEnter the first number: ";
cin >> n;
cout << "Enter the second number: ";
cin >> m;
cout << endl;
if (friends(n, m)){
cout << n << " and " << m << " are friends" << endl;
}
else{
cout << n << " and " << m << " aren't friends" << endl;
}
break;
case 2:
cout << "\nEnter a number: ";
cin >> n;
cout << "\n";
printVertically(n);
break;
case 3:
cout << "\nThe first number must be higher than second number" <<
endl;
cout << "\nEnter the first number: ";
cin >> n;
cout << "Enter the second number: ";
cin >> m;
cout << endl;
if (n > m){
cout << "C(" << n << ", " << m << ") = " << choice(n, m) <<
endl;
}
else{
cout << "Doesn't meet the main condition" << endl;
}
break;
}
}
}