# Golang program to print left pascals triangle

In this Golang article, we will learn how to print left Pascal’s triangle by using recursion and iterative method. Pascal's triangle is a triangular array of binomial coefficients. The left pascal's triangle is a variant of Pascal's triangle that is obtained by reflecting Pascal's triangle along a vertical axis.

## Syntax

func leftPascal(n, k int) int{…}


The leftPascal() function is used to print left Pascal’s triangle. It takes two arguments as input.

func leftPascal(n int){…}


The leftPascal() function is used to print left Pascal’s triangle. It takes one argument, number of rows as input.

## Algorithm

• Step 1 − First, we need to import the fmt package.

• Step 2 − Start the main() function. Inside the main() function, define the number of rows for creating the left Pascal’s triangle.

• Step 3 − Create a nested loop to iterate the each row and column of the triangle. The outer loop iterates through the rows and the inner loop iterates through the columns.

• Step 4 − Now, call the leftPascal() function to calculate the value at each position.

• Step 5 − Further, the resultant left Pascal’s triangle is printed on the screen by using the fmt.Println() function.

• Step 6 − Now, define a leftPascal() function that is used to print the left Pascal’s triangle.

• Step 7 − It provides a binomial coefficient value for each position in the triangle and represents the number of ways to choose k items for a set of n items.

• Step 8 − It checks if k is equal to 0 or n, in which case the binomial coefficient is 1. If k is not equal to 0 or n, the function calculates the binomial coefficient using the formula: C(n, k) = C(n-1, k-1) + C(n-1, k)

• Step 9 − Then, by calling this function for each position in the triangle, it prints the left half of the Pascal’s triangle.

## Example 1

In this example, we will define a leftPascal() function using iterative method that is used to print left Pascal’s triangle.

package main

import "fmt"

func main() {
rows := 5

fmt.Printf("The resultant left Pascal's triangle is: \n")
for i := 0; i < rows; i++ {
for j := 0; j <= i; j++ {
fmt.Printf("%d ", leftPascal(i, j))
}
fmt.Println()
}
}

func leftPascal(n, k int) int {
if k == 0 || k == n {
return 1
} else {
return leftPascal(n-1, k-1) + leftPascal(n-1, k)
}
}


## Output

The resultant left Pascal's triangle is:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1


## Example 2

In this example, we will define a leftPascal() function using recursive method that is used to print left Pascal’s triangle.

package main

import "fmt"

func pascalValue(n, k int) int {
if k == 0 || k == n {
return 1
}
return pascalValue(n-1, k-1) + pascalValue(n-1, k)
}

func leftPascal(n int) {
for i := 0; i < n; i++ {
for j := 0; j <= i; j++ {
fmt.Print(pascalValue(i, j), "")
}
fmt.Println()
}
}

func main() {
fmt.Printf("The resultant left Pascal's triangle is: \n")
leftPascal(5)
}


## Output

The resultant left Pascal's triangle is:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1


## Conclusion

We have successfully compiled and executed a go language program to print left Pascal’s triangle by using recursion and iterative methodalong with two examples. In the first example, we have used the iterative method and in the second example, we have used the recursive method.

Updated on: 10-May-2023

60 Views