Write logic to print Floyd's triangle in PHP?

devquora
devquora

Posted On: Feb 22, 2018

 

Floyd’s triangle is the right-angled triangle which starts with 1 and filled its rows with a consecutive number. The count of elements in next row will increment by one and the first row contains only one element.

Example of Floyd's triangle having 4 rows

The logic to print Floyd's triangle

<?php

echo "print Floyd's triangle"; echo "<pre>
$key = 1; for ($i = 1; $i <= 4; $i++) { for ($j = 1; $j <= $i; $j++) { echo $key; $key++; if ($j == $i) { echo "<br/>"; } } } echo ""; ?>

Output:

1

2 3

4 5 6

7 8 9 10

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    PHP Interview Questions

    What is T_PAAMAYIM_NEKUDOTAYIM in PHP?

    T_PAAMAYIM_NEKUDOTAYIM is scope resolution operator used as :: (double colon) .Basically, it used to call static methods/variables of a Class...

    PHP Interview Questions

    What is the difference between == and === operator in PHP ?

    In PHP == is equal operator and returns TRUE if $a is equal to $b after type juggling and === is Identical operator and return TRUE if $a is equal to $b, and they are of the same data type...