Print a gradual number of characters

Asked

Viewed 68 times

-2

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

*

**

***

****

*****

******

*******

  • 1

    You can start editing with a more objective and short title. Adding your attempt to solve the problem as well.

  • Hi Diego I improved the post!

  • 1

    You can improve it a lot more... Don’t use the title as a description. And what did Voce try? Add your code as well.

  • Tell us what your attempts have been. The purpose is to help you, not to do for you

2 answers

3

I understand that as it is an exercise, the @Diegof answer is the most correct for the question.

Just as a reference, you get a version using str_repeat, most suitable if it is for an actual application:

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

See working on IDEONE.

If you are going to use HTML, change the "\n" for '<br>', or even by "<br>\n" (and duplicate if you want the blank lines).

1

You can use 2 loops, the outermost controls the line change and the other prints the respective amount of * of that line.

Behold:

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

See working on IDEONE. Since you didn’t make it clear if this line spacing was part of it, I added an extra break, so the exit:

*

**

***

****

*****

******

*******
  • It would be interesting who gave the downvote show me where is the problem in the answer. It meets the requested.

  • Maybe the person did not like you deliver the exercise ready, but I do not think it is reason for -1. Anyway, I skipped.

  • @Bacco then is only implicância even, because the user does not understand how ties works, naes editions he cited that does not understand "cycles. But on the other hand, I have sinned in not explaining what is happening, I will complement.

  • 1

    Even so I think dirty the -1, I understand that mine is only a complement, for exercise your better fulfills the purpose.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.