This Python code example calculates the combination and permutation of two user-provided integers, `n`

and `r`

. It defines a recursive function, `fact`

, to compute the factorial of a given number.

```
def fact(val):
# Base case: If val is 0 or 1, return 1 as the factorial of 0 and 1 is 1
if val == 0 or val == 1:
return 1
else:
# Recursive case: Return val multiplied by the factorial of val-1
return val * fact(val - 1)
# Prompt the user to enter the value of n and convert it to an integer
n = int(input("Enter value of n : "))
# Prompt the user to enter the value of r and convert it to an integer
r = int(input("Enter value of r : "))
# Calculate the combination using the formula nCr = n! / (r!(n-r)!)
combination = int(fact(n) / (fact(r) * fact(n - r)))
# Calculate the permutation using the formula nPr = n! / (n-r)!
permutation = int(fact(n) / fact(n - r))
# Print the calculated combination value
print("\nCombination : " + str(combination))
# Print the calculated permutation value
print("Permutation : " + str(permutation))
```

```
Enter value of n : 4
Enter value of r : 3
Combination : 4
Permutation : 24
```

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

**Function Definition**:`def fact(val):`

defines a function named`fact`

that takes a single argument`val`

.**Base Case**:`if val == 0 or val == 1: return 1`

checks if`val`

is 0 or 1. If true, it returns 1 because the factorial of 0 or 1 is 1.**Recursive Case**:`else: return val * fact(val - 1)`

computes the factorial of`val`

by multiplying`val`

by the factorial of`val - 1`

. This is a recursive call where the function calls itself with a decremented value of`val`

.

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

**Input for**:`n`

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

prompts the user to enter a value for`n`

, converts the input to an integer, and assigns it to the variable`n`

.**Input for**:`r`

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

prompts the user to enter a value for`r`

, converts the input to an integer, and assigns it to the variable`r`

.

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

**Combination Calculation**:`fact(n)`

: Computes the factorial of`n`

.`fact(r)`

: Computes the factorial of`r`

.`fact(n - r)`

: Computes the factorial of`n - r`

.`fact(n) / (fact(r) * fact(n - r))`

: Computes the combination formula (ππ)=π!π!(πβπ)!(*r**n*β)=*r*!(*n*β*r*)!*n*!β.`int(...)`

: Converts the result to an integer and assigns it to the variable`combination`

.

**Permutation Calculation**:`fact(n)`

: Computes the factorial of`n`

.`fact(n - r)`

: Computes the factorial of`n - r`

.`fact(n) / fact(n - r)`

: Computes the permutation formula π(π,π)=π!(πβπ)!*P*(*n*,*r*)=(*n*β*r*)!*n*!β.`int(...)`

: Converts the result to an integer and assigns it to the variable`permutation`

.

```
print("\nCombination : " + str(combination))
print("Permutation : " + str(permutation))
```

**Printing Combination**:`print("\nCombination : " + str(combination))`

prints the string “Combination :” followed by the value of`combination`

.**Printing Permutation**:`print("Permutation : " + str(permutation))`

prints the string “Permutation :” followed by the value of`permutation`

.

Login

Accessing this tutorial requires a login. Please enter your credentials below!