Pyramid of asterisks in php [duplicate] - php

This question already has answers here:
How can I create a pyramid from using php?
(14 answers)
Closed 8 years ago.
I am having problem creating a pyramid of asterisk.
Please see my code.
<?php
for($i=1;$i<=5;$i++){
for($j=1;$j<=$i;$j++){
echo "*";
}
echo "<br />";
}
?>
Result:
*
**
***
****
*****
My question is how I am going to make that like.
*
* *
* * *
* * * *
* * * * *

<pre><?php
$n = $i = 5;
while ($i--)
echo str_repeat(' ', $i).str_repeat('* ', $n - $i)."\n";
?></pre>

use the same program within <center> </center> tag ! like:
<center>
<?php
for($i=1;$i<=5;$i++){
for($j=1;$j<=$i;$j++){
echo "*";
}
echo "<br />";
}
?>
</center>

Use HTML whitespace character to procude the whitespaces:
So something like this:
<?php
// pyramid height
$height = 5;
for($i=1;$i<=$height;$i++){
for($t = 1;$t <= $height-$i;$t++)
{
echo " ";
}
for($j=1;$j<=$i;$j++)
{
// use here to procude space after each asterix
echo "* ";
}
echo "<br />";
}
?>

try this
$height = 5;
$space = $height;
for($i = 1; $i <= $height; $i++) {
echo str_repeat(' ', --$space);
for($j=1;$j<=$i;$j++){
if($j > 1) {
echo ' ';
}
echo '*';
}
echo '<br />';
}

create_pyramid("*", 5);
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;
}
}
From link posted above by Baba

$n = 5;
$i = 0;
for($i=1; $i<=$n; $i++){
echo "<pre>";
echo str_repeat(" ", $n-$i);
echo str_repeat("# ", $i);
}

Related

I want to print triangle in stars pattern in PHP can anyone help, Just using two for loops only to solve it?

Please give me the solution for to print a triangle in stars pattern using php language. I used only 2 for loops to finish this.
*
***
*****
*******
Here is my code
<?php
for($a=1;$a<=7;$a++)
{
for($b=1;$b<=$a;$b++)
{
echo " *";
}
echo "<br/>";
}
echo " ";
?>
Please help me out.
<?php
for($i=0; $i < 5 ; $i++)
{
echo"<br>";
for($j=0; $j <=$i ; $j++){
echo "*" ;
}
}
?>
For more php Star patterns
This will work, still uses two for loops:
echo "<pre>";
$stars = "*";
for($a=1; $a<=4; $a++)
{
$spaces = "";
for($b = (5 - $a); $b>1; $b--)
{
$spaces .= " ";
}
echo $spaces . $stars . $spaces;
$stars .= "**";
echo "<br/>";
}
echo "</pre>";
I've used an accumulator for $stars, it starts from one star, an adds two stars at each iteration, and one accumulator for $spaces, that gets reset at each main for iteration.
The triangle is build top to bottom using one layer at a time.
One layer is made of spaces, stars and spaces again
for($x = 0; $x < $n; $x++) {
for($y = 0; $y < $n - $x; $y++) {
echo ' ';
}
for($z = 0; $z < $x * 2 +1; $z++) {
echo 'x';
}
echo "\n";
}

How can I output this with a simple PHP for loop?

How can I output these with a simple 'for' loop?
####*
###**
##***
#****
*****
*****
#****
##***
###**
####*
hashtag = spaces
I've tried to changing the * and the 'br' within this one:
for($i = 0; $i < 5; $i++){
for($b = 0; $b <= $i; $b++) {
echo '*';
}
echo "</br>";
}
But than it will just output them under each other >.>
for($i=1;$i<6;$i++){
for($k=0;$k<5-$i;$k++){
echo " ";
}
for($j=0;$j<$i;$j++){
echo "*";
}
echo "<br>";
}
echo "<br>";
for($i=0;$i<6;$i++){
for($j=0;$j<$i;$j++){
echo " ";
}
for($k=0;$k<5-$i;$k++){
echo "*";
}
echo "<br>";
}
My Solution. Next time you should do your homework by yourself ;-)

I Want to print Star pattern

I want to print star pattern like :
below is my code. but it doesn't work for me. How can i fix this problem.
for($i=1; $i<=5; $i++){
for($j=5; $j>$i; $j--){
echo " ";
}
echo "*";
for($j=1; $j<($i-1)*2; $j++){
echo " ";
}
if($i==1)echo "<br>";
else echo "*<br>";
}
for($i=4; $i>=1; $i--){
for($j=5; $j>$i; $j--){
echo " ";
}
echo "*";
for($j=1; $j<($i-1)*2; $j++){
echo " ";
}
if($i==1)echo "<br>";
else echo "*<br>";
}
?>
You should put all text that should keep form and not whitespace trimmed in between a <pre/> tag
<pre><?php
for($i=1; $i<=5; $i++){
for($j=5; $j>$i; $j--){
echo " ";
}
echo "*";
for($j=1; $j<($i-1)*2; $j++){
echo " ";
}
if($i==1)echo "<br>";
else echo "*<br>";
}
for($i=4; $i>=1; $i--){
for($j=5; $j>$i; $j--){
echo " ";
}
echo "*";
for($j=1; $j<($i-1)*2; $j++){
echo " ";
}
if($i==1)echo "<br>";
else echo "*<br>";
} ?></pre>
Note: if your target is console and PHP-CLI, don't put the <pre/> tag, omit the end of PHP tag (?>) and replace all <br/> tags with \n.
Thanks to all of you guys. I add <pre> tag before code and also change <br> tag with \n and now it is work. Thanks again. Have a nice day guys..
Try this...
function printPattern($n)
{
$k = 0;
// Print upper triangle
for ($i = 1; $i <= $n; $i++)
{
// Print spaces
for ($j = 1; $j <= $n - $i; $j++)
{
echo " ";
}
// Print #
while ($k != (2 * $i - 1))
{
if ($k == 0 or $k == 2 * $i - 2)
echo "*";
else
echo " ";
$k++;
}
$k = 0;
// move to next row
echo "\n";
}
$n--;
// Print lower triangle
for ($i = $n; $i >= 1; $i--)
{
// Print spaces
for ($j = 0; $j <= $n - $i; $j++)
{
echo " ";
}
// Print #
$k = 0;
while ($k != (2 * $i - 1))
{
if ($k == 0 or $k == 2 * $i - 2)
echo "*";
else
echo " ";
$k++;
}
echo "\n";
}
}
// Driver Code
$n = 6;
printPattern($n);
// This Code is contributed by mits
?>

Pyramid asterisk in PHP

Please see my code.
for ($row = $var; $row >= 1; --$row) {
for($j=0;$j<$row;++$j)
{echo "*";}
echo "</br>";
echo "&nbsp";
Output:
*****
****
***
**
*
But i need output as below:
*****
****
***
**
*
You might want to check out some of the string functions:
<?php
for ($i = 5; $i > 0; $i--) {
echo str_repeat(' ', 5 - $i).str_repeat('*',$i).PHP_EOL;
}
for ($i = 5; $i > 0; $i--) {
echo str_pad(str_repeat('*',$i),5,' ',STR_PAD_LEFT).PHP_EOL;
}
This runs on the command line, like so:
php filename.php
<?php
$var = 5;
echo "<div style='font-family:Courier New, Courier, monospace;'>";
for ($row = $var; $row >= 1; $row--) {
for($i=$row;$i<$var;$i++)
{
echo "&nbsp";
}
for($j=0;$j<$row;$j++)
{
echo "*";
}
echo "<br>";
}
echo "</div>";
?>

How can I create a pyramid from using php?

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);
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
^_^^_^^_^
^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
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);
<?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>";
}
?>
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);
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/>";
}
?>
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:
*
***
*****
*******
*********
<?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>";
}
?>
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);
function pyramid($height)
{
for ($i = 1; $i <= $height; $i++) {
echo str_repeat(" ", $height - $i);
echo str_repeat('*', $i * 2 - 1) . '<br/>';
}
}
pyramid(5);
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> ";
}
?>
$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/>';
}
}
}
}
}
}
$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 " &nbsp";
}
}
echo "<br/>";
}
<?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>

Categories