Advertisement
Virajsinh

1 2 3 6 5 4 7 8 9 12 11 10 Pyramid Print in PhP

Jan 10th, 2024 (edited)
1,086
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.80 KB | Source Code | 0 0
  1. <?php
  2.  
  3. $row = 4;
  4. $start = 1;
  5. $i = 1;
  6.  
  7. for ($i; $i <= $row; $i++)
  8. {
  9.     if($i % 2 == 0)
  10.     {
  11.         $first = $start + 2;
  12.         $sec = $start + 1;
  13.         printf("%d %d %d <br>", $first, $sec, $start);
  14.         $start = $start + 3;
  15.     }else{
  16.         printf("%d %d %d<br>", $start, $start+1, $start+2);
  17.         $start = $start + 3;
  18.     }
  19. }
  20.  
  21. // Output
  22. // 1 2 3
  23. // 6 5 4
  24. // 7 8 9
  25. // 12 11 10
  26.  
  27. Alternative
  28.  
  29. $rows=4;
  30. $cols=10; // Any Numbers Of Column
  31. $count=1;
  32.  
  33. for($i=1; $i<=$rows; $i++)
  34. {
  35.     if($i%2==0){
  36.         $count=$count+$cols-1;
  37.     }
  38.     for($j=$cols; $j>=1; $j--)
  39.     {
  40.         echo $count." ";
  41.         if($i%2==0)
  42.             $count--;
  43.         else
  44.             $count++;
  45.     }
  46.     if($i%2==0){
  47.         $count=$count+$cols+1;
  48.     }
  49.     echo "<br>";
  50. }
  51. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement