Butterfly Pattern in PHP of Numbers
Butterfly pattern in php of numbers, In this article, you will learn how to print the butterfly pattern in php of numbers.
Example
This the butterfly pattern:
1 1
1 2 2 1
1 2 3 3 2 1
1 2 3 4 4 3 2 1
1 2 3 4 5 5 4 3 2 1
1 2 3 4 5 6 6 5 4 3 2 1
1 2 3 4 5 6 7 7 6 5 4 3 2 1
1 2 3 4 5 6 6 5 4 3 2 1
1 2 3 4 5 5 4 3 2 1
1 2 3 4 4 3 2 1
1 2 3 3 2 1
1 2 2 1
1 1
You should have knowledge of the following topics in PHP programming to understand this program:
- PHP
for
loop statement
In this program, we used normal functions and statements to print the butterfly pattern.
Source Code
<?php
// Butterfly Pattern in PHP of Numbers
// Size of the butterfly pattern
$h = 7;
echo "This the butterfly pattern:\n\n";
// This will print the butterfly pattern
butterflyPattern($h);
// It's the function to
// print the butterfly pattern
function butterflyPattern($h) {
for($r = 1; $r <= $h - 1; $r++) {
echo "\t";
for($d = 1; $d <= $r; $d++)
echo $d . " ";
for($s = 1; $s <= 2 * ($h - $r); $s++)
echo " ";
for($d = $r; $d >= 1; $d--)
echo $d . " ";
echo "\n";
}
for($r = $h; $r >= 1; $r--) {
echo "\t";
for($d = 1; $d <= $r; $d++)
echo $d . " ";
for($s = 1; $s <= 2 * ($h - $r); $s++)
echo " ";
for($d = $r; $d >= 1; $d--)
echo $d . " ";
echo "\n";
}
}
?>
Output
This the butterfly pattern:
1 1
1 2 2 1
1 2 3 3 2 1
1 2 3 4 4 3 2 1
1 2 3 4 5 5 4 3 2 1
1 2 3 4 5 6 6 5 4 3 2 1
1 2 3 4 5 6 7 7 6 5 4 3 2 1
1 2 3 4 5 6 6 5 4 3 2 1
1 2 3 4 5 5 4 3 2 1
1 2 3 4 4 3 2 1
1 2 3 3 2 1
1 2 2 1
1 1
Explanation
In this program, we have made the calculation using the for
loop statement.