PHP Script to print Floyd’s Triangle

Floyd’s Triangle is a right-angled triangle filled with rows of consecutive numbers. It starts with 1:

Example of 4 rows of Floyd’s Triangle:

1
2 3
4 5 6
7 8 9 10

Let’s see how to print Floyd’s Triangle using PHP and HTML:

<html>
<head>
<title>PHP Script to Print Floyd's Traingle</title>
</head>
<body>
<h3>PHP Script to print Floyd's Traingle</h3>
<form method="post">
Enter the limit: <input type="number" name="limit" /> 
<input type="submit" value="Print Floyd's Traingle" /><br /><br />
<?php
//check if the post data has some value 
if(isset($_POST['limit']) && $_POST['limit']){
	$limit = $_POST['limit'];
	$a = 1;
	
	for ($i = 1; $i <= $limit; $i++){
		for ($c = 1; $c <= $i; $c++){
			echo $a.' ';
			$a++;
		}
		echo "<br>";
	}
}
?>
</form>
</body>
</html>

It’s a pretty easy program just see the above script to understand how it works.

You can also see the demo of this program in the below link:

demo

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top