How to use Simple Loop In PHP
One of the simplest ways to generate Pascal’s Triangle is by using nested loops. This approach iterates through each row and column, calculating the binomial coefficient for each position.
PHP
<?php function pascalsTriangle( $rows ) { for ( $i = 0; $i < $rows ; $i ++) { $number = 1; for ( $j = 0; $j <= $i ; $j ++) { echo $number . " " ; $number = $number * ( $i - $j ) / ( $j + 1); } echo "\n" ; } } // Driver code $rows = 5; pascalsTriangle( $rows ); ?> |
Output
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
PHP Program to Print Pascal’s Triangle
Pascal’s Triangle is a mathematical construct named after the French mathematician Blaise Pascal. It is a triangular array of binomial coefficients, where each number is the sum of the two directly above it. In this article, we will explore different approaches to print Pascal’s Triangle in PHP.
Table of Content
- Using Simple Loop
- Using Recursion
- Using Dynamic Programming
Contact Us