PHP program to print Triangle number in reverse pattern

1. Program to print Triangle number in reverse pattern

<?php
for($i=6;$i>=1;$i--)
 {
   for($j=$i;$j>=1;$j--)
    {
     echo $j." ";
     if($j==1)
     {
       echo " ";
       echo "<br />";
     }
   }
} 
?>

Output:

6 5 4 3 2 1 
5 4 3 2 1 
4 3 2 1 
3 2 1 
2 1 
1

2. Program to print Triangle number in reverse pattern

<?php 
for($i=1;$i<=6;$i++)
{
for($j=$i;$j>=1;$j--)
 { echo $j." ";
   if($j==1)
     {
       echo " ";
       echo "<br />";
     }
  }
 } 
?>

Output:

1 
2 1 
3 2 1 
4 3 2 1 
5 4 3 2 1 
6 5 4 3 2 1
4 Comments
  1. Tanvi Parekh says

    33333
    6666
    999
    1212
    15

    give this pattern pro.

    1. Full Stack developer says

      Hello Tanvi Parekh,

      Please try this code:

      <?php
      $num = 3;
      for ($i = 1; $i <= 6; $i++) {
          for ($j = 6 - $i; $j >= 1; $j--) {
              echo $i * $num . " ";
          }
          echo "<br>";
      }
      ?> 
      
  2. vishal says

    Hello sir,
    1 2 3 4
    8 7 6 5
    9 10 11 12
    16 15 14 13

    give me this pattern ans.

    1. Full Stack developer says

      Hello Vishal,

      Try this code –

      $k = 1;
      for ($i = 1; $i <= 4; $i++) {
        if ($i % 2 == 0) { // check here row start with even number
              $h = 4 * $i;
              for ($j = 1; $j <= 4; $j++) {
                  echo $h.' ';
                  $h--;
                  $k++;
              }
              echo "<br>";
          } else {
              for ($j = 1; $j <= 4; $j++) {
                  echo $k.' ';
                  $k++;
              }
              echo "<br>";
          }
      }
      

Leave A Reply

Your email address will not be published.