- 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 Star Pascals Triangle
In this tutorial, we will learn how to print star Pascal’s triangle using Go programming language
Example 1: Golang Code To Print Star Pascal Triangle Using Strconv Package
Syntax
func Itoa(x int) string
Itoa() function takes in an integer parameter and returns the string representation of x when the base is 10
Algorithm
Step 1 − Import the package fmt and strconv package.
Step 2 − Start function main ().
Step 3 − Declare and initialize the variables.
Step 4 − Use of for loop with condition and incrementor.
Step 5 − Print the result using fmt.Println ().
Example
// GOLANG PROGRAM TO PRINT STAR PASCALS TRIANGLE package main // fmt package provides the function to print anything // Package strconv implements conversions to and from // string representations of basic data types import ( "fmt" "strconv" ) // start function main () func main() { // declare the variables var i, j, rows, num int // initialize the rows variable rows = 7 // Scanln() function scans the input, reads and stores //the successive space-separated values into successive arguments fmt.Scanln(&rows) fmt.Println("GOLANG PROGRAM TO PRINT STAR PASCALS TRIANGLE") // Use of For Loop // This loop starts when i = 0 // executes till i<rows condition is true // post statement is i++ for i = 0; i < rows; i++ { num = 1 fmt.Printf("%"+strconv.Itoa((rows-i)*2)+"s", "") // run next loop as for (j=0; j<=i; j++) for j = 0; j <= i; j++ { fmt.Printf("%4d", num) num = num * (i - j) / (j + 1) } fmt.Println() // print the result } }
Output
GOLANG PROGRAM TO PRINT STAR PASCALS TRIANGLE 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
Description Of The Code
In the above program, we first declare the package main.
We imported the fmt package that includes the files of package fmt. We also imported Package strconv implements conversions to and from string representations of basic data types.
Now start the function main().
Declare the four integer variables i, j, num and rows. Initialize the rows variable to an integer value you want for Pascal’s triangle pattern. Use fmt.Scanln() to read and store the rows value.
Using for loop: The condition is given inside an if statement and stop execution is mentioned once the condition is right.
In the line 28 of the code: The loop starts when i = 0 and executes till i<rows condition is true and the post statement is i++.
In the line 32 of the code: it runs the next loop as for (j=0; j<=i; j++).
In this loop the function strconv.Itoa() is called for the calculation of pascal’s triangle.
And last printing the result in the form of a triangle on the screen using fmt.Println.
Conclusion
We have successfully compiled and executed the Golang program code to print star Pascal’s triangle in the above example.