Bu dersimizde VB.NET'te kombinasyon hesaplayan programı yazacağız.


İlk olarak programın görüntüsünü oluşturalım:






Daha sonra değişkenlerimizi oluşturalım:




Kod:

Dim pay, payda As Double
Dim n, r As Integer
Daha sonra hesaplamamızı yapabilmek için btnKombinasyonHesapla_Click Olayını yazalım:


Kod:
Try
pay = 1
payda = 1

If txtn.Text <> "" And txtr.Text <> "" Then
n = Integer.Parse(txtn.Text)
r = Integer.Parse(txtr.Text)
If n >= r Then


pay = Double.Parse(n, Globalization.NumberStyles.Integer)
For i = 1 To r - 1


pay *= Double.Parse((n - i).ToString())


Next
For i = 1 To r
payda *= Double.Parse(i.ToString(), Globalization.NumberStyles.Integer)
Next




lblSonuc.Text = (pay / payda).ToString()






Else
MsgBox("r , n'den büyük olamaz..!", MsgBoxStyle.Exclamation, Title:="Uyarı..!")


End If
ElseIf txtn.Text = "" Then
MsgBox("n sayısını giriniz..!", MsgBoxStyle.Exclamation, Title:="Uyarı..!")
ElseIf txtr.Text = "" Then
MsgBox("r sayısını giriniz..!", MsgBoxStyle.Exclamation, Title:="Uyarı..!")
End If






Catch hata As Exception
MsgBox(hata.Message.ToString(), MsgBoxStyle.SystemModal, "Hata..!")


Finally
pay = 1
payda = 1
n = 1
r = 1


End Try
Kod:
Public Class Form1
Dim pay, payda As Double
Dim n, r As Integer


Private Sub btnKombinasyonHesapla_Click(sender As Object, e As EventArgs) Handles btnKombinasyonHesapla.Click
Try
pay = 1
payda = 1

If txtn.Text <> "" And txtr.Text <> "" Then
n = Integer.Parse(txtn.Text)
r = Integer.Parse(txtr.Text)
If n >= r Then


pay = Double.Parse(n, Globalization.NumberStyles.Integer)
For i = 1 To r - 1


pay *= Double.Parse((n - i).ToString())


Next
For i = 1 To r
payda *= Double.Parse(i.ToString(), Globalization.NumberStyles.Integer)
Next




lblSonuc.Text = (pay / payda).ToString()






Else
MsgBox("r , n'den büyük olamaz..!", MsgBoxStyle.Exclamation, Title:="Uyarı..!")


End If
ElseIf txtn.Text = "" Then
MsgBox("n sayısını giriniz..!", MsgBoxStyle.Exclamation, Title:="Uyarı..!")
ElseIf txtr.Text = "" Then
MsgBox("r sayısını giriniz..!", MsgBoxStyle.Exclamation, Title:="Uyarı..!")
End If






Catch hata As Exception
MsgBox(hata.Message.ToString(), MsgBoxStyle.SystemModal, "Hata..!")


Finally
pay = 1
payda = 1
n = 1
r = 1


End Try
End Sub



End Class