Print a gradual number of characters

-2

Using a repeat structure, print the following on the screen:

*

**

***

****

*****

******

*******

    
asked by anonymous 21.04.2016 / 03:24

2 answers

3

I understand that since this is an exercise, @DiegoF's answer is the correct one for the question.

Just for reference, there is a version using str_repeat , more suitable if it is for an actual application:

for( $i = 0; $i < 7; ++$i ) echo str_repeat( '*', $i ) . "\n";

See working at IDEONE .

If it is to be used in HTML, change "\n" to '<br>' , or even "<br>\n" (and duplicate if you want the lines blank).

    
21.04.2016 / 15:55
1

You can use 2 loops, the outer one controls the row change and another prints the respective amount of * of that row.

See:

for($i = 0; $i < 7; $i++){
    for($j = 0; $j <= $i; $j++){
        echo "*";
    }
    echo "\n\n";
}

See working at IDEONE . As you did not make it clear if this line spacing was part of it, I added one more break, so the output stays:

*

**

***

****

*****

******

*******
    
21.04.2016 / 13:21