OneCompiler

PHP program to print Reverse Pyramid with alphabets

242

Following php program builds reverse pyramid pattern with Alphabets using for loop

<?php

function pyramid($n)
{

	for ($i = $n; $i >= 1; $i--)
	{

		for ($gap = $n - 1; $gap >= $i;
							$gap--)
		{
			echo"  ";
		}

		$num = 65;

		for ($j = 1; $j <= $i; $j++)
		{
			echo chr($num++)." ";
		}

		for ($j = $i - 1; $j >= 0; $j--)
		{
			echo chr(--$num)." ";
		}
		echo"\n";
	}
}
function lastRow($n){
  $la=$n-2;
  for ($gap = 0; $gap <= $la;$gap++)
  {
		echo"  ";
	}
	echo" A";
}

$n = 5;
pyramid($n);
lastRow($n);

?>

Output:

A B C D E E D C B A 
  A B C D D C B A 
    A B C C B A 
      A B B A 
        A A 
         A

Try it Online here https://onecompiler.com/php/3x73thcmq