2

The output to my code should be:

*
**
***
****
*****

I'm currently using this code with a nested for loop to get the result.

for($lineNumber=1;$lineNumber<=5;$lineNumber++) {
    for($starCount=1;$starCount<=$lineNumber;$starCount++)         {
            echo("*");
    }
        echo("<br />");
}

I need to be able to get the same result without the nested for loop, and I'm stumped. The only thing I want to use is a single for loop. Nothing else. No ifs, switches, or other loops.

Thanks!

0

5 Answers 5

5
$str = '';
for($lineNumber=1;$lineNumber<=5;$lineNumber++) {
    $str = $str . '*';
    echo $str;
    echo("<br />");
}

Using this string accumulator removes the need for a second loop.

0
2

Use this:

for($lineNumber=1;$lineNumber<=5;$lineNumber++) {
    echo str_repeat("*",$lineNumber);
    echo("<br />");
}
5
  • str_repeat still uses a loop internally. Doesn't this just hide that from the code without changing anything?
    – CollinD
    Sep 15, 2015 at 15:37
  • @CollinD i dont think so. its an internal loop. although the case is not this and he wants his code styling to be clearer. Sep 15, 2015 at 15:38
  • 1
    I suppose you're right on that count. I just figured this was for homework (looks like a Freshman assignment haha)
    – CollinD
    Sep 15, 2015 at 15:39
  • I dont get what is Freshman assignment :D cause im not native english speaker (maybe its an idiom, maybe you are making fun of me .. :)) really?). Anyway I think my code is clearer and I think its the most important thing in coding. Sep 15, 2015 at 15:44
  • not making fun of anyone, this just looks like an assignment that would be given as homework in a freshman level college programming course.
    – CollinD
    Sep 15, 2015 at 15:44
1

Here is recursion version of it.

function inner($num)
{
    if ($num == 0)
        return;
    echo "*";
 
    inner($num - 1);
}
 
function outer($n, $i)
{
    if ($n == 0)
        return;
    inner($i);
    echo "<br/>";
    outer($n - 1, $i + 1);
}
 
$n = 5;
outer($n, 1);
0

These are some examples of drawing a pyramid:

function print($n)
{
    //example 1
    for ($x = 1; $x <= $n; $x++) {
        for ($y = 1; $y <= $x; $y++) {
            echo 'x';
        }
        echo "\n";
    }

    // example 2
    for ($x = 1; $x <= $n; $x++) {
        for ($y = $n; $y >= $x; $y--) {
            echo 'x';
        }
        echo "\n";
    }

    // example 3

    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";
    }


}
0

As of your requirement, that is fine to use with concat the string but what if string is not same as previous? here is solution for each patterns without any loop.

$i = 0;
a:
if($i <= 5){
    $j = 1;
    b:
    if($j<=$i){
        echo $i;
        $j++;
        goto b;
    }
    echo "<br/>";
    $i++;
    goto a; 
}

This will print

1
22
333
4444
55555

You can make any pattern like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.