HOW TO PRINT A PASCAL TRIANGLE USING VBA CODE

Share Now

Problem Statement

Now today we are going to create Pascal Triangle using VBA. This is not a reporting problem statement but in some of the organizations in an interview, they ask this question to understand the programming capabilities

This is an interesting nested loop exercise

Approach

In the Pascal triangle, every element makes sum of the element directly above it and the element to the left of the element directly above it

The table will fill in series of numbers starting from 1 to 25. In each row, there are five numbers.
To the run the example program, you will need one Form and one Command Button. SOLUTION

Option Explicit

Private Sub CommandButton1_Click()

Dim i(10, 10) As Integer
Dim r, j As Integer
i(0, 0) = 1
i(1, 0) = 1
i(1, 1) = 1
For r = 2 To 10
i(r, 0) = 1
For j = 1 To r

i(r, j) = i(r – 1, j – 1) + i(r – 1, j)
Next
Next
For r = 0 To 10

For j = 0 To r
Cells(r + 1, j + 1) = i(r, j)
Next
Next
End Sub

STEPS TO PERFORM

• Go to developer Tab else Press Alt + F11
• Click on Visual Basic icon
• Go to Insert click on Module Share Now
March 19, 2019

2 responses on "HOW TO PRINT A PASCAL TRIANGLE USING VBA CODE"

1. Hello.This article was really interesting, especially since I was searching for thoughts on this topic lazt week.

2. Outstanding post, I believe people should acquire a lot ffrom this blg its real user friendly.
So much superb information on here :D. 