Home >Backend Development >PHP Tutorial >PHP implements Floyd's triangle
This article introduces how to use PHP to generate and display the first n rows of Floyd's triangle. (Use lines n=5 and n=11)
Freud's triangle is a set of right-angled triangular natural numbers used in computer science education. It is named after Robert Floyd. It is defined as filling the rows of a triangle with consecutive numbers, starting with 1 in the upper left corner:
PHP code example is as follows:
<?php $n = 5; echo "n = " . $n . "\n"; $count = 1; for ($i = $n; $i > 0; $i--) { for ($j = $i; $j < $n + 1; $j++) { printf("%4s", $count); $count++; } echo "\n"; }
Output:
Related recommendations: "PHP Tutorial"
This article is about An introduction to the method of implementing Floyd's triangle in PHP. It is simple and interesting. I hope it will be helpful to friends in need!
The above is the detailed content of PHP implements Floyd's triangle. For more information, please follow other related articles on the PHP Chinese website!