Excel Crazy

HOW TO PRINT A PASCAL TRIANGLE USING VBA CODE

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
January 30, 2018

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

Leave a Message

Your email address will not be published. Required fields are marked *

top
 

© LOKESH PALIWAL 1987 - 2018

LOKESH@EXCELCRAZY[DOT]COM | 8826547882

Reporting Solutions | Data Consulting | Analytics Training

CONTACT US

Your Name (required)

Your Email (required)

Your Phone no. (required)

Your Message for us...

Skip to toolbar