Triangle Patterns

by Devansh


Posted on 20 Oct 2018 05:10:04 (1 month ago)




There are many triangle patterns which commonly asked in interviews. We are using PHP to print the pattern. Kindly see and help us to improve the algos.

/*

 Pattern 1

     *
    * *
   * * *
  * * * *

*/
function triangle1($rows) {
    $number_of_rows = $rows;
    $spaces = $number_of_rows;
    for ($i=1; $i<=$number_of_rows; $i++) {
        for ($j=1; $j<=$spaces; $j++) {
            echo "&nbsp;";
        }
        $spaces--;
        for ($k=1; $k<=$i; $k++) {
            echo "*" . "&nbsp;";
        }
        echo "<br>";
    }
}
echo triangle1(4);
/*

 Pattern 2

  *
  * *
  * * *
  * * * *

*/
function triangle2($rows) {
    $number_of_rows = $rows;
    for ($i=1; $i<=$number_of_rows; $i++) {
        for ($j=1; $j<=$i; $j++) {
            echo "*";
        }
        echo "<br>";
    }
}
echo triangle2(4);
/*

 Pattern 3

        *
      * *
    * * *
  * * * *

*/
function triangle3($rows) {
    $number_of_rows = $rows;
    $flag = $number_of_rows;
    for ($i=1; $i<=$number_of_rows; $i++) {
        for ($j=1; $j<=$number_of_rows; $j++) {
            if ($j >= $flag) {
                echo "*";
            } else {
                echo "&nbsp;&nbsp;";
            }
        }
        $flag--;
        echo "<br>";
    }
}
echo triangle3(4);
/*

 Pattern 4

     * * * * *
       * * *
         * 

*/
function triangle4() {
    $num =5;
    for ($i=0;$i<3;$i++){
        for ($j=0;$j<$i;$j++){
            echo "&nbsp;&nbsp;";
        }
        for ($k=0;$k<$num;$k++){
            echo "*";
        }
        $num = $num-2;
        echo "<br>";
    }
}
echo triangle4();


There are some number patterns which is also in shape of traingles.

/*

 Pattern 5

     1
    2 2
   3 3 3
  4 4 4 4

*/
function triangle5($rows) {
    $number_of_rows = $rows;
    $spaces = $number_of_rows;
    for ($i=1; $i<=$number_of_rows; $i++) {
        for ($j=1; $j<=$spaces; $j++) {
            echo "&nbsp;";
        }
        $spaces--;
        for ($k=1; $k<=$i; $k++) {
            echo $i . "&nbsp;";
        }
        echo "<br>";
    }
}
echo triangle5(4);
/*

 Pattern 6

     1
    1 2
   1 2 3
  1 2 3 4

*/
function triangle6($rows) {
    $number_of_rows = $rows;
    $spaces = $number_of_rows;
    for ($i=1; $i<=$number_of_rows; $i++) {
        for ($j=1; $j<=$spaces; $j++) {
            echo "&nbsp;&nbsp;";
        }
        $spaces--;
        for ($k=1; $k<=$i; $k++) {
            echo $k . "&nbsp;";
        }
        echo "&nbsp;";
    }
}
echo triangle6(4);
/*

 Pattern 7

     1
    2 3
   4 5 6
  7 8 9 10

*/
function triangle7($rows) {
    $num=1;
    $number_of_rows = $rows;
    $spaces = $number_of_rows;
    for ($i=1; $i<=$number_of_rows; $i++) {
        for ($j=1; $j<=$spaces; $j++) {
            echo "&nbsp;";
        }
        $spaces--;
        for ($k=1; $k<=$i; $k++) {
            echo $num . "&nbsp;";
            $num++;
        }
        echo "<br>";
    }
}
echo triangle7(4);
/*

 Pattern 8

      1
     3 2
    4 5 6
  10 9 8 7

*/
function triangle8($rows) {
    $num=1;
    $number_of_rows = $rows;
    $spaces = $number_of_rows;
    for ($i=1; $i<=$number_of_rows; $i++) {
        for ($j=1; $j<=$spaces; $j++) {
            echo "&nbsp;";
        }
        $spaces--;
        if ($i%2==0) {
            $num = $num + $i - 1;
            for ($k=1; $k<=$i; $k++) {
                echo $num . "&nbsp;";
                $num--;
            }
            $num = $num + $i + 1;
        } else {
            for ($k=1; $k<=$i; $k++) {
                echo $num . "&nbsp;";
                $num++;
            }
        }
        echo "<br>";
    }
}
echo triangle8(4);