Jump to content

Pascal's triangle: Difference between revisions

Line 1,986:
1 2 1
1 3 3 1
</pre>
Another solution:
<lang Go>
package main
 
import "fmt"
 
func main() {
fmt.Println("Enter the number of rows: ")
var n int
fmt.Scanf("%d", &n)
for r := 0; r < n; r++ {
for i := 1; i < n-r; i++ {
fmt.Print(" ")
}
j := 0
k := 1
for j <= r {
if k%2 != 0 {
fmt.Print(C(r, j))
j++
} else {
fmt.Print(" ")
}
k++
}
for i := 1; i < n-r; i++ {
fmt.Print(" ")
}
fmt.Println()
}
}
 
func C(r int, j int) int {
return factorial(r) / (factorial(j) * factorial(r-j))
}
 
func factorial(m int) int {
if m == 0 {
return 1
} else {
return m * factorial(m-1)
}
}
</lang>
Output:
<pre>
Enter the number of rows:
5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
</pre>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.