I need to create a pyramid using asterisks. I specify a value which becomes the base of the pyramid. The base contains as much asterisks as the value specified and the pyram
function pyramid($height)
{
for ($i = 1; $i <= $height; $i++) {
echo str_repeat(" ", $height - $i);
echo str_repeat('*', $i * 2 - 1) . '<br/>';
}
}
pyramid(5);
$n1=10;
$k=$n1/2;
$n=$n1/2;
for($i=0 ; $i<=$n1 ; $i++) {
if($i <= $n1/2) {
$k=$k+1;
$n=$n-1;
} else {
$k=$k-1;
$n=$n+1;
}
for($j=0 ; $j<=$n1 ; $j++) {
if($j < $k && $j > $n) {
echo "*";
} else {
echo "  ";
}
}
echo "<br/>";
}
Diamond Shape
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
<?php
$num=15;
$num2=0;
$half = $num;
for($i=$num2; $i<=$num; $i++)
{
for($j=$half; $j>$i; $j--)
{
echo " ";
}
for($j=$num2; $j<=$i; $j++)
{
echo " * ";
}
echo "<br>";
}
for($i=$num2; $i<=$num; $i++)
{
for($j=$num2; $j<=$i; $j++)
{
echo " ";
}
for($j=$half; $j>$i; $j--)
{
echo " * ";
}
echo "<br> ";
}
?>
I think that the simplest solution is to create 2 loops with condition:
$n = 5; // specify how many rows you want to
$stars = 0;
for ($i = $n; $i > 0; $i--) {
for ($j = 0; $j < $i + $stars; $j++) {
if ($j < $i - 1) {
echo " ";
} else {
echo "*";
}
}
$stars += 2;
echo "\n";
}
The output will be:
*
***
*****
*******
*********
I prefer mine :
echo '<pre>';
$n = 5;
function print_tree($n, $str, $max) {
for ($i = 0; ($i < (($max - $n) / 2)); $i++) {
echo " ";
}
for ($i = 0; ($i < $n); $i++) {
echo $str;
}
echo "<br/>";
}
for ($flag = 0; ($flag < 2); $flag++) {
for ($a = 1, $b = 1, $c = 1, $d = 4; (($d - 3) <= $n); $a += 2, $b++) {
if ($flag == 1) {
print_tree($a, "*", $max);
}
if ($b == $d) {
if ($flag == 0) {
$max = $a;
}
if (($d - 3) != $n) {
$a -= ((2 * $c) + 2);
}
$b = 0;
$d++;
if (($d % 2) == 0) {
$c++;
}
}
}
}
if ((($foot = $n) % 2) == 0) {
$foot++;
}
for ($i = 0; ($i < $foot); $i++) {
print_tree($foot, "|", $max);
}
outputs :
*
***
*****
*******
*****
*******
*********
***********
*************
***********
*************
***************
*****************
*******************
*********************
*****************
*******************
*********************
***********************
*************************
***************************
*****************************
*************************
***************************
*****************************
*******************************
*********************************
***********************************
*************************************
***************************************
|||||
|||||
|||||
|||||
|||||
Or even this one:
<?php
$n = 8;
ob_start();
$stars = ($n - 1) * 2 + 1;
$spaces = 0;
for ($i = 0; ($i < $n); $i++) {
echo str_repeat(' ', $spaces);
echo str_repeat('*', $stars);
echo ' ';
echo str_repeat(' ', $spaces * 2);
echo str_repeat('*', $stars);
echo "\n";
$spaces += 1;
$stars -= 2;
}
$stars = ($n - 1) * 2 + 1;
$spaces = 0;
$margin = $stars / 2 + 1;
for ($i = 0; ($i < $n); $i++) {
echo str_repeat(' ', $margin);
echo str_repeat(' ', $spaces);
echo str_repeat('*', $stars);
echo "\n";
$spaces += 1;
$stars -= 2;
}
echo trim(implode("\n", array_reverse(explode("\n", ob_get_clean()))), "\n"), "\n";
it gives:
*
***
*****
*******
*********
***********
*************
***************
* *
*** ***
***** *****
******* *******
********* *********
*********** ***********
************* *************
*************** ***************
funny exercices isn't it... 8-)
You can try
create_pyramid("*", 5);
create_pyramid("@", 10);
create_pyramid("^_^", 10);
function create_pyramid($string, $level) {
echo "<pre>";
$level = $level * 2;
for($i = 1; $i <= $level; $i ++) {
if (!($i % 2) && $i != 1)
continue;
print str_pad(str_repeat($string, $i),($level - 1) * strlen($string), " " , STR_PAD_BOTH);
print PHP_EOL;
}
}
Output A
*
***
*****
*******
*********
Output B
@
@@@
@@@@@
@@@@@@@
@@@@@@@@@
@@@@@@@@@@@
@@@@@@@@@@@@@
@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@
Output C
^_^^_^^_^
^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^