- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to Print Right Pascals Triangle
A pascals triangle is a form of triangle in which binomial coefficients are arranged in triangular form. Here, the triangle starts with 1 and in every row the beginning and ending digit is 1. In this article, we will write Golang programs to print right pascals triangle.
Demonstration
This demonstration explains a right pascal triangle, in which every row shows the coefficient of binomial expansion for the power of (a+b)^n, where a and b =1. The first row has single 1, second row has 1 and 1 , the third row has 1, 2 and 1 and so on.
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1
Algorithm
Step 1 − Import the fmt and main package in the program where fmt helps in the formatting of the input and output and the main ensures that the program should be an executable program
Step 2 − Create a main function and in that function create a shorthand variable row and assign 8 to it which means the right-angled triangle will be made of 8 rows
Step 3 − Use an outer for loop using I variable iterating till the rows and in the loop assign the val equal to 1
Step 4 − Use an inner loop with j variable less than equal to i variable. In the loop print the val and update it in every inner iteration
Step 5 − After the inner iteration use a Println function which is used to add new line to the triangle
Step 6 − Finally, the right angled triable will be printed on the console
Example 1
In this example, two nested for loops are used to print the right pascals triangle. In the outer for loop val will be set to a value which will be updated later in the inner loop. In this way, the output will be printed on the console.
package main import "fmt" func main() { rows := 8 for i := 0; i< rows; i++ { val := 1 for j := 0; j <= i; j++ { fmt.Printf("%d ", val) val = val * (i - j) / (j + 1) } fmt.Println() } }
Output
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1
Example 2
In this example, two loops will be used inside an outer for loop. Here, especially array will be used to store the calculated elements of the row which will be calculated using second for loop.
package main import "fmt" //Main function to execute the program func main() { rows := 8 var array [8]int for i := 0; i< rows; i++ { array[0] = 1 for j := i; j > 0; j-- { array[j] += array[j-1] } for l := i; l >= 0; l-- { fmt.Print(array[l], " ") } fmt.Println() } }
Output
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1
Conclusion
We have successfully compiled and executed the program of printing the right pascals triangle. In the first example, we used two nested for loops to print the triangle and in the second example, we used two loops inside an outer loop with array considered a row.