PHP implements Floyd's triangle

藏色散人
Release: 2023-04-05 17:32:01
Original
3548 people have browsed it

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 implements Floyd's triangle

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";
   }
Copy after login

Output:

PHP implements Floyds triangle

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!