Public Class Form1
Private Sub ShowFactorial(ByVal n As Integer)
Dim product As Integer = 1
Do While n > 1
product = product * n
n = n - 1
Loop
MessageBox.Show(product)
End Sub
Private Sub btnFact_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFact.Click
ShowFactorial(Val(tbNum.Text))
End Sub
' Because the ShowFactorial subroutine uses a BYVAL parameter,
' You CANNOT use it to calculate the number of combinations
Private Sub btnCombos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCombos.Click
' The number of combinations is calculated using
' the following formula: N! / ( R! * (N-R)! )
Dim n As Integer = Val(tbN.Text)
Dim r As Integer = Val(tbR.Text)
Dim nMinusR As Integer = n - r
Dim factN As Integer = 1
Dim factR As Integer = 1
Dim factNMinusR As Integer = 1
Dim numCombinations As Integer
Do While n > 1
factN = factN * n
n = n - 1
Loop
Do While r > 1
factR = factR * r
r = r - 1
Loop
Do While nMinusR > 1
factNMinusR = factNMinusR * nMinusR
nMinusR = nMinusR - 1
Loop
numCombinations = factN / (factR * factNMinusR)
MessageBox.Show(numCombinations)
End Sub
End Class