Hello readers, today I have taken example of Floyds triangle. Floyd's triangle is a right-angled triangle of array having natural numbers. The natural numbers are printed in such a manner , that it will look like right angle triangle. It is named after Mathematician Robert Floyd. Floyd's triangle is made by filling the rows of the triangle with consecutive natural numbers, starting with 1 in the top left corner.
Here is the program to print Floyd's triangle
<html>
<body>
<form method="post" action="index.php">
<label>Enter the number :</label><input type="text" name="number"><br/>
<input type="submit" name="submit">
</form>
<?php
$a=1;
$k=htmlspecialchars($_POST["number"]);
for($i=1; $i<=$k; $i++){
for($j=1;$j<=$i;$j++){
echo $a;
$a++;
}
print("<br/>");
}
?>
</body>
</html>
Output:
1
23
456
78910
1112131415
161718192021
22232425262728
2930313233343536
373839404142434445
46474849505152535455
Comment : In the above example I took an inputbox, in which user entered a number, this number will assign to variable 'k'. Now I have applied for loop which will start from 0, and ends upto variable 'k'(user entered number). Inside for loop, I have used another for loop which will start from 1 and ends at the value of variable 'i'. Inside this loop I am printing variable 'a' whose starting value is 1 and then incrementing with the for loop.
0 Comment(s)