1

Stuck with the number pattern printing logic. Let me know what i am doing wrong as my file is simply going on execution without giving me a pattern.

My Code --

<?php

$num = 4;

for( $j=1 ; $j <= $num ; $j++ )
{
    for( $i=$j ; $i < $num-1 ; $i++ )
    {
    echo "&nbsp;";
    }

    for( $j ; $j >= 1 ; $j-- )
    {
    echo $j."&nbsp;";
    }

echo "<br />";
}

Pattern to achieve --

   1
  21
 321
4321

UPDATE

After applying new changes following are the screenshots ---

On STAR USEAGE

ON BLANK USAGE

swapnesh
  • 26,318
  • 22
  • 94
  • 126

7 Answers7

5
for ($i = 1; $i <= 4; $i++) {
  echo str_pad(implode('', range($i, 1)), 4, ' ', STR_PAD_LEFT) . '<br />';
}

Right aligned using CSS

echo '<div style="text-align:right;">';
for ($i = 1; $i <= 4; $i++) {
  echo implode('', range($i, 1)) . '<br />';
}
echo '</div>';
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • 1
    +1 for the str_pad method, could you please tell me what is wrong in the above code ..to help me to build my logic :) – swapnesh Aug 21 '12 at 06:32
  • I guess it's because you use `$j` for the outer and the second inner loop. – KingCrunch Aug 21 '12 at 06:34
  • I don't think will result his expectation. – Mihai Iorga Aug 21 '12 at 06:42
  • @MihaiIorga you are right its not printing in the right order check my updated post – swapnesh Aug 21 '12 at 06:44
  • @KingCrunch please check my updated post with screenshot ..blank spaces are not placing correctly – swapnesh Aug 21 '12 at 06:47
  • 1
    @swapnesh They are placed correctly, but I guess you execute it in your browser? Depending on what this is good for I recommend the `text-align`-css-property, but you can `str_replace(' ', '$nbsp;', $string)` too (if you like, but alignment is not what the fixed-length whitespaces meant to be used for). – KingCrunch Aug 21 '12 at 07:05
2

Your error is in the last for, that should not exist since you are already looping.

And create a new variable which will hold the printed text for the next increment.

<?php

$num = 4;
$wrap = '';

for( $j=1 ; $j <= $num ; $j++ )
{
    for( $i=$j ; $i < $num ; $i++ )
    {
        echo "&nbsp; ";
    }

    echo $wrap = $j.$wrap;
    echo "<br />";
}
?>
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
2

The fundamental reason is that a browser wont render multiple spaces only the first one, you can overcome this by using the non breaking space html entity &nbsp in place of spaces.

Soooo, If you want your actual pattern to look like:

   1
  21
 321
4321

And not like:

1
21
321
4321

Use &nbsp (Edit: Tho actually use &nbsp;[space] as just 1 seems to not compensate for the width of the 1 char vs 4)

<?php 
    $num = 4;
    $result = array();
    foreach(range($num,1) as $i){
        $result[] = str_repeat('&nbsp; ',$num-$i).implode('',range($i,1)).'<br />';
    }
    echo implode('',array_reverse($result));
?>

Or you could use a <pre> tag like:

<?php 
$num = 4;
$result = array();
foreach(range($num,1) as $i){
    $result[] = str_repeat(' ',$num-$i).implode('',range($i,1)).PHP_EOL;
}
echo '<pre>'.implode('',array_reverse($result)).'</pre>';
?>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
1

As it is right now, the problem lies in your third (or second nested) for loop.

You can't just reuse $j as a counter here, since $j is still being actively used in the encompassing for loop. Substitute that for loop with:

for( $k = $j ; $k >= 1 ; $k-- )
{
    echo $k."&nbsp;";
}
ryuusenshi
  • 1,976
  • 13
  • 15
1

Here is the code for your program, You can go to Develepor hell for more such pattern

// Outer look for line change

for ($i = 1; $i<=5; $i++) {

// Loop added for spacing
for ($s = $i; $s<=10-$i; $s++) {
    echo "&nbsp;";
    }

// Inner loop for printing required pattern
for ($j = $i; $j>=1; $j--) {
    echo $j;
}

echo '</br>';

}

arushi
  • 404
  • 5
  • 15
0
// Here is the code for your program,
// Pattern - 1

$num = 4;

// to print no. of rows
for( $i=1 ; $i <= $num ; $i++ )
{

    
    // To print white space
    for( $j=1 ; $j <= $num-$i ; $j++ )
    {
        echo "0";
    }
    
    // To print pattern
    for( $z= $i ; $z>=1 ; $z-- )
    {
        
        echo $z;
    }
    
    
    // To print new line
    echo "<br />";
}

==>> Output
0001
0021
0321
4321

//===================================================================//

Pattern - 2

$z=1;
$n=3;

# no of rows
for($i=1;$i<=$n;$i++)
{
# to check even or odd
if($i%2 == 0)
{
    # main logic to display in reverse order // right to left
    $z = ($z+$n) -1;

    $a = $z;
    for($j=1;$j<=$n;$j++)
    {
        echo $z--;
    }

    $z = $a+1;


}
else
{
    # display data left to right

    for($j=1;$j<=$n;$j++)
    {
        echo $z++;
    }
}

echo "<br/>";

}

==>> Output
123
654
789 
Sonu Chohan
  • 141
  • 1
  • 5
0

Your issue appears to be that you are printing your output into an HTML document which condenses multiple spaces as its default behavior. See "Why does HTML require that multiple spaces show up as a single space in the browser?" To "preserve" whitespaces while printing, use the <pre> tag.

I'll add a math-based solution instead of multiple iterated function calls and multiple loops (inspired by my explained answer here). Once your number limit gets higher than 9, I don't know if my output will align with your desired output.

Effectively, I use printf() to left-pad every line with spaces to the same max length. Each successive number prints the integer as many times as its value.

Code: (Demo)

$num = 4;
echo "<pre>";
for ($i = 1; $i <= $num; ++$i) {
    printf("% {$num}d<br>", (10 ** $i - 1) / 9 * $i);
}

HTML Output (click on the eye icon in the demo link to switch to HTML mode):

   1
  22
 333
4444

For comparison these also work inside of the for() loop (and offer a more intuitive result above 9):

printf("% {$num}d<br>", str_repeat($i, $i));

Or

echo str_repeat(' ', $num - $i) . str_repeat($i, $i) . "<br>";
mickmackusa
  • 43,625
  • 12
  • 83
  • 136