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
14 Comments
  1. Tanvi Parekh says

    33333
    6666
    999
    1212
    15

    give this pattern.

    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>";
          }
      }
      
  3. siva says

    hello sir
    6,1,1,1,1,1
    2,4,2,2,2,2
    1,1,6,1,1,1
    2,2,2,4,2,2
    1,1,1,1,6,1
    2,2,2,2,2,4
    give me this pattern ans.

    1. siva says
      <?php
      for ($i = 1; $i<= 6; $i++) {
          for ($j = 1; $j<= 6; $j++) {
              if($i%2 == 0){
                  if($i == $j){
                      echo "4";
                  } else {
                      echo "2";
                  }
              } else {
                  if($i == $j){
                      echo "6";
                  } else {
                      echo "1";
                  }
      
              }
          }
          echo "<br>";
      }
      ?>
      
      1. Vikas Kumar says

        Dear Siva,
        Thank you so much for the useful solution.

  4. murshid says

    1
    2 6
    3 7 10
    4 8 11 13
    5 9 12 14

    1. murshid says

      give php code it.please help

      1. Vikas Kumar says

        Try this code –

        <?php
        $n = 5;
        for ($i = 1; $i <= $n; $i++) {
            $diff  = $n - 1;
            $value = $i;
            for ($j = 1; $j <= $i; $j++) {
                echo $value . " ";
                $value = $value + $diff;
                $diff--;
            }
            echo "<br />";
        }
        ?>
        
  5. Saranya E R says

    A
    C D
    G H I
    M N O P
    U V W X Y

    can you explain the PHP code for this pattern and hw it works?

    1. Vikas Kumar says

      Try this code –

      <?php
      $num = 65;
      for ($i = 0; $i < 5; $i++) {
          $k = 1;
          for ($j = 0; $j <= $i; $j++) {
              
              if ($k == 1) {
                  $num = $num + $i;
              }
              $ch = chr($num);
              echo $ch . " ";
              $num = $num + 1;
              $k++;
          }
          echo "<br />";
      }
      ?>
      
  6. Saranya ER says

    Thanku so much…😊

    1. Vikas Kumar says

      You’re welcome. Happing Coding.

Leave A Reply

Your email address will not be published.