Python Code Example: Permutation and combination of 2 numbers

This code calculates the combination and permutation of two numbers “n” and “r”. The function “fact(val)” calculates the factorial of a given number “val”.

The code takes two inputs “n” and “r” from the user and uses them to calculate the combination and permutation of the two numbers. The combination of “n” and “r” is calculated by dividing the factorial of “n” by the product of the factorial of “r” and the factorial of “n-r”. The permutation of “n” and “r” is calculated by dividing the factorial of “n” by the factorial of “n-r”.

Finally, the code prints out the combination and permutation of the two numbers.

def fact(val):
    if val == 0 or val == 1:
        return 1
    else:
        return val * fact(val - 1)


n = int(input("Enter value of n : "))
r = int(input("Enter value of r : "))

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

print("\nCombination : " + str(combination))
print("Permutation : " + str(permutation))
Output
Enter value of n : 4
Enter value of r : 3

Combination : 4
Permutation : 24