Permutation and combination of 2 numbers

This code calculates the number of combinations and permutations possible when given two integers n and r.

Combination is the number of ways to choose r items from a set of n items. Permutation is the number of ways to arrange r items from a set of n items.

Here, the fact function is used to calculate the factorial of a given number val. The factorial of a number n is calculated by multiplying n with n-1 until we reach 1. The fact function uses a recursive approach to calculate the factorial of a number.

In the main function, the user is asked to enter the values of n and r. The combination is calculated by dividing the factorial of n by the product of factorials of r and n-r. The permutation is calculated by dividing the factorial of n by the factorial of n-r. Finally, the values of combination and permutation are displayed to the user.

#include <iostream>
using namespace std;

int fact(int val) {
    if (val == 0 || val == 1)
        return 1;
    else
        return val * fact(val - 1);
}

int main() {
    int n, r, combination, permutation;

    cout << "Enter value of n : ";
    cin >> n;
    cout << "Enter value of r : ";
    cin >> r;

    combination = fact(n) / (fact(r) * fact(n - r));
    permutation = fact(n) / fact(n - r);

    cout << "\nCombination : " << combination << endl;
    cout << "Permutation : " << permutation;

    return 0;
}
Output
Enter value of n : 9
Enter value of r : 6

Combination : 84
Permutation : 60480