Wednesday, November 10, 2010

Calculating combinations C (n, k)

Make a label 4, 2 and textbox CommandButton 1, and you design like the image below




Function Faktorial(ByVal n As Integer) As Double
Dim i As Integer, Fakt As Double
Fakt = 1
For i = 2 To n
Fakt = Fakt * i
Next
Faktorial = Fakt
End Function

Function kombinasi(ByVal n As Integer, ByVal k As Integer) As Long
kombinasi = Faktorial(n) / (Faktorial(n - k) * Faktorial(k))
End Function

Private Sub cmdhitung_click()
Dim n As Integer, k As Integer
n = Txtn.Text
k = Txtk.Text
LblCnk.Caption = kombinasi(n, k)
End Sub