designersvsoft
designersvsoft

Reputation: 1859

Print text in the shape of two triangles

I am just a beginner in PHP. I am trying to write program to print numbers like following.

1        1
12      21
123    321
1234  4321
1234554321

I have written the following code.

<?php

$n=5;
for($i=1; $i<=$n; $i++)
{
    echo "<br />";
    for($j=1; $j<=$i; $j++)
    {
        echo $j;
    }
}

?>

The result displays the following.

1
12
123
1234
12345

I could not reverse it like

    1
   21
  321
 4321
54321

How can I do this?

Upvotes: 1

Views: 1733

Answers (5)

Tudor Constantin
Tudor Constantin

Reputation: 26861

A more generic solution:

<?php
$n = 5;
$seq1 = '';
$seq2 = '';
$format1 = sprintf("%%-%su", $n); //right justified with spaces
$format2 = sprintf("%%%su", $n); //left justified with spaces

for($i=1; $i<=$n;$i++){
  $seq1 .= $i;
  $seq2 = strrev($seq1);
  echo sprintf("$format1$format2\n", $seq1, $seq2);
}

Upvotes: 1

Timur
Timur

Reputation: 6718

<div style="position:relative;width:100px;height:auto;text-align:right;float:left;">
<?php

$n=5;
for($i=1; $i<=$n; $i++)
{
    echo "<br />";
    for($j=1; $j<=$i; $j++)
    {
        echo $j;
    }
}

?>
</div>

Upvotes: 0

Fenec
Fenec

Reputation: 1222

$n = 5;

for ($i = 1; $i <= $n; $i++) {
 $counter .= $i;
 $spaces = str_repeat("&nbsp;", ($n-$i)*2);  
 echo $counter . $spaces . strrev($counter) . "<br/>";
}

Upvotes: 0

Lucas
Lucas

Reputation: 10646

Here is my solution to your problem.

It isn't the best solution because it doesn't take into account that you could be using numbers higher than 9, in which case it will push the numbers out of line with each other.

But the point is that it is still the start of a solution that you could work on if needed.

You can use an array to store the numbers you want to print. Because the numbers are in an array it means we can just use a foreach loop to make sure all of the numbers get printed.

You can use PHP's str_repeat() function to figure out how many spaces you need to put in between each string of numbers. The below solution will only work if you use an array with the default number indicies as opposed to an associative array. This is because it uses the $key variable in part of the calculation for the str_repeat() function. If you would rather not use the $key variable then you should be able to figure out how to change that.

When it come to reversing the numbers they have already been stored in a string so you can just use PHP's strrev() function to take care of that and store them in another variable.

Finally you just have to print a line to the document with a line break at the end.

Note that the str_repeat() function is repeating the &nbsp; HTML entity. This is because the browser will just compress normal white space down to 1 character.

Also note that I have included a style block to change the font to monospace. This is to ensure that the numbers all line up with each other.

<style>
    html, body {
        font: 1em monospace;
    }
</style>
<?php
$numbers = array(1, 2, 3, 4, 5);
$numbers_length = count($numbers);
$print_numbers = '';
$print_numbers_rev = '';

foreach($numbers as $key => $value) {
    $spaces = str_repeat('&nbsp;', ($numbers_length - ($key + 1)) * 2);
    $print_numbers .= $value;
    $print_numbers_rev = strrev($print_numbers);

    echo $print_numbers . $spaces . $print_numbers_rev . '<br />';
}

Edit:

Solution without array:

<style>
    html, body {
        font: 1em monospace;
    }
</style>
<?php
$numbers = 9;
$numbers_length = $numbers + 1;
$print_numbers = '';
$print_numbers_rev = '';

for($i = 0; $i <= $numbers; ++$i) {
    $spaces = str_repeat('&nbsp;', ($numbers_length - ($i + 1)) * 2);
    $print_numbers .= $i;
    $print_numbers_rev = strrev($print_numbers);

    echo $print_numbers . $spaces . $print_numbers_rev . '<br />';
}

Upvotes: 0

Amadan
Amadan

Reputation: 198446

Okay. What you wrote is pretty good. There need to be several changes in order to do what you wanted though. The first problem is that you are rendering it to HTML - and HTML does not render spaces (which we'll need). Two solutions: you use &nbsp; for space, and make sure you use a proportional font, or you wrap everything into a <pre> tag to achieve pretty much the same thing. So, echo "<pre>"; at the start, echo "</pre>"; at the end.

Next - don't have the inner loop go to $i. Let it go to 5 every time, and print a number if $j <= $i, and a space otherwise.

Then, right next to this loop, do another one, but in reverse (starting with 5 and ending with 1), but doing the very same thing.

Viola is a musical instrument.

Upvotes: 1

Related Questions