Alphabet Triangle using PHP method
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
<?php
function printPattern($rows) {
for ($i = 1; $i <= $rows; $i++) {
// Print spaces
for ($space = 1; $space <= $rows - $i; $space++) {
echo " ";
}
// Print characters in ascending order
for ($j = 0; $j < $i; $j++) {
echo chr(65 + $j);
}
// Print characters in descending order
for ($j = $i - 2; $j >= 0; $j--) {
echo chr(65 + $j);
}
echo "\n";
}
}
$rows = 5; // Number of rows for the pattern
printPattern($rows);
?>
Number Triangle:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
<?php
function printNumberTriangle($rows) {
for ($i = 1; $i <= $rows; $i++) {
// Print numbers
for ($j = 1; $j <= $i; $j++) {
echo $j . " ";
}
echo "\n";
}
}
$rows = 5; // Number of rows for the triangle pattern
printNumberTriangle($rows);
?>
Upward-Pointing Star Triangle:
*
***
*****
*******
*********
<?php
function printUpwardStarTriangle($rows) {
for ($i = 1; $i <= $rows; $i++) {
// Print spaces
for ($space = 1; $space <= $rows - $i; $space++) {
echo " ";
}
// Print stars
for ($j = 1; $j <= 2 * $i - 1; $j++) {
echo "*";
}
echo "\n";
}
}
$rows = 5; // Number of rows for the upward-pointing star triangle
printUpwardStarTriangle($rows);
?>
Downward-Pointing Star Triangle:
*********
*******
*****
***
*
<?php
function printDownwardStarTriangle($rows) {
for ($i = $rows; $i >= 1; $i–) {
// Print spaces
for ($space = 1; $space <= $rows – $i; $space++) {
echo ” “;
}
// Print stars
for ($j = 1; $j <= 2 * $i – 1; $j++) {
echo “*”;
}
echo “\n”;
}
}
$rows = 5; // Number of rows for the downward-pointing star triangle
printDownwardStarTriangle($rows);
?>