Reputation: 6269
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 pyramid must skip its rows by 1..Here I am facing a problem when I specify an even number of base..
The pyramid must looke like the one below.
*
***
*****
*******
*********
**********
I am getting
####*
###***
##*****
###*****
####*****
**********
I want to replace the # by some blank space and I am getting the bug that the number of asterisks in the 4th row has decreased.. How do I fix these two bugs ?
function create_pyramid($limit){
if ($limit > 0){
for ($row =0;$row<=$limit;$row++){
if (($row % 2 == 0) && ($row != $limit)){ continue;}
$rows = "";
for ($col =0;$col<$row;$col++){
$rows= $rows.'*';
}
$pattern = "%'#".((($limit - $row)/2)+$row)."s\n";
printf ($pattern,$rows);
print '<br />';
}
}
else{
print "Invalid data";
}
}
create_pyramid(10);
Upvotes: 4
Views: 16109
Reputation: 1
$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/>";
}
Upvotes: 0
Reputation: 454
$n = 5;
for($i = $n; $i >= 1; $i--){
for($j = 1; $j <= $i; $j++){
if($j < $i){
echo '0';//you can replace 0 with space
}else{
$num = $n - $i;
for($k = 0; $k <= $num; $k++){
echo '*';
}
for($y = 1; $y <= $n - 1; $y++){
if($y <= $num){
echo '*';
}else{
echo '0';//you can replace 0 with space
if($y == $n - 1){
echo '<br/>';
}
}
}
}
}
}
Upvotes: 0
Reputation: 1
<?php
ini_set('display_errors', 1);
/**
* Here is my solution for printing the pyramid using a class
*/
class pyramid
{
function __construct($rows)
{
for ($k=1; $k <= $rows ; $k++) {
$this->space($rows,$k);
$this->left($k);
$this->right($k);
echo "<br>";
}
}
public function left($k)
{
for ($i=1; $i < $k ; $i++) {
echo $i;
}
}
public function right($i)
{
for ($j=$i; $j >= 1 ; $j--) {
echo $j;
}
}
public function space($rows,$k)
{
for ($i=$rows-$k; $i > 0 ; $i--) {
echo " ";
}
}
}
$pyramid = new pyramid(5);
?>
<style type="text/css">
body{
font-family: monospace;
font-size: 24px;
}
</style>
Upvotes: -1
Reputation: 27
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> ";
}
?>
Upvotes: 0
Reputation: 514
function pyramid($height)
{
for ($i = 1; $i <= $height; $i++) {
echo str_repeat(" ", $height - $i);
echo str_repeat('*', $i * 2 - 1) . '<br/>';
}
}
pyramid(5);
Upvotes: 0
Reputation: 2517
function create_piramide ($filas) {
$n = $filas;
echo '<pre>';
for ($i = $filas; $i >= 0; $i--) {
echo str_repeat(' ', $i).str_repeat('o ', $n - $i)."\n";
}
echo '</pre>';
}
create_piramide (10);
Upvotes: 0
Reputation: 33
<?php
$l=5;
for ($i=1; $i <=5; $i++) {
for ($k=1; $k < $l ; $k++) {
echo ' ';
}
for ($j=0; $j<$i; $j++) {
echo '*';
}
$l--;
echo "<br>";
}
?>
Upvotes: 0
Reputation: 146
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:
*
***
*****
*******
*********
Upvotes: 0
Reputation: 36984
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-)
Upvotes: 10
Reputation: 167
From what I understand, what you are looking for is an Odd numbered pyramid, i.e. the number of * in each row is as per odd number series, like 1,3,5,7. If you wish to include "if-else" statements then you can go with the above answered loops, but if you only wish to use "for" loops, then you can use the following code:
<?php
$x=1;
for($i=1;$i<7;$i++)
{
for($j=7;$j>$i;$j--)
{
echo ' ';
}
for($k=1;$k<=$x;$k++)
{
echo '*';
}
$x=$x+2;
echo "<br/>";
}
?>
Upvotes: 0
Reputation: 3020
Just make it simpler:
function create_pyramid($limit) {
for($row = 1; $row < $limit; $row ++) {
$stars = str_repeat('*', ($row - 1) * 2 + 1);
$space = str_repeat(' ', $limit - $row);
echo $space . $stars . '<br/>';
}
}
echo "<pre>" ;
create_pyramid(10);
Upvotes: 6
Reputation: 17020
function create_row($num, $limit) {
$append = '';
if($limit > $num && ($limit - $i) % 2 == 0) {
$append .= '-';
}
$stars = str_repeat('*', $num);
$ap_len = floor(($limit - $num) / 2);
$prepend = str_repeat('-', $ap_len);
$append .= str_repeat('-', $ap_len);
return $prepend . $stars . $append;
}
function create_pyramid($limit){
if ($limit > 0){
$no_last = false;
for($i = 1; $i <= $limit; $i += 2) {
print create_row($i, $limit) . PHP_EOL;
if($i == $limit) {
$no_last = true;
}
}
if(!$no_last) {
print create_row($limit, $limit) . PHP_EOL;
}
}
}
create_pyramid(10);
Upvotes: 0
Reputation: 13785
<?php
$n=9;
for($i=0; $i<=$n; $i++)
{
for($j=1; $j<=$i; $j++)
echo " ";
for($k=1; $k<=$n-$i; $k++)
echo $k;
for($j=($k-2); $j>0; $j--)
echo $j;
for($k=1; $k<=$i; $k++)
echo " ";
echo "</br>";
}
?>
Upvotes: 1
Reputation: 95151
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
^_^^_^^_^
^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
Upvotes: 8