SETHA SITE INFORMATION

  • Increase font size
  • Default font size
  • Decrease font size

Pascal Triangle in Visual Basic 2008

E-mail Print PDF
The code below produce pascal triangle using Visual Basic 2008 programming language. This sample allows user to enter value of n.
Public Class Form1
    Private Function Factoriel(ByVal n As Integer) As Long
        If n = 0 Then
            Return 1
        Else
            Return n * Factoriel(n - 1)
        End If
    End Function

    Private Sub btnPascal_Click(ByVal sender As System.Object, _
                                ByVal e As System.EventArgs) _
                                Handles btnPascal.Click
        Me.Refresh()
        Dim g As Graphics = Me.CreateGraphics
        Dim w As Integer = Me.Width
        Dim x As Long
        For i As Integer = 0 To Me.txtN.Text
            For j As Integer = 0 To i
                x = Factoriel(i) / (Factoriel(j) * Factoriel(i - j))
                g.DrawString(x, New Font("Arial", 10, FontStyle.Regular), _
                             Brushes.Black, w / 2 - 20 * i + 40 * j, i * 20)
            Next
        Next
    End Sub
End Class
Download Source Code Here