![]() ![]() The lower half– which prints the decreasing number of stars. The upper half – which prints the increasing number of stars. To print this pattern, we would need to divide the pattern into two halves. The number of stars increases until the maximum defined and then decrease back to 1, giving us a full diamond shape. This pattern prints a complete diamond shape on the screen. Remember, how we used double spacing to give a finished look to our pattern? Here we would use single spacing so that the stars are alternately aligned in odd and even number of rows, giving us a true triangular pattern. Revisit the Note in Mirrored Half Pyramid. Although there is a slight twist when we code it. This pattern is essentially a combination of Half pyramid and its mirror. Or in other words, it prints a triangle of stars on the screen. I believe by now you would be able to guess the logic for this one. The number of stars decreases with each line and stars are right-aligned. This pattern is an indented inverted half pyramid. So, for inverted patterns such as this, we know that the number of stars is more in the first line. This means we can either loop from 0 to 5 or we can loop in decreasing order from 5 to 0. Although, what can be changed is how we start the loops, increasing or decreasing order. Keeping the logic in mind, we know that the outer loop always has to control the number of lines and the inner loop has to control the number of stars. The first line has 5 stars, the second line has 4 stars and so on. We will use this to our leverage when we print full pyramids.įor this pyramid pattern, the number of stars keeps decreasing with each new line. This gives a finished look to our pattern rather than a congested print. This is because we print a single-space along with the stars as well. Note: Keep one thing in mind that the number of spaces in the k-loop is double space. This is how we control our inner loop based on the outer loops control variable. Our outer control variable is i and inner control variable is j.Īnd so on. Since the number of stars in each line is directly dependent on the line number, our inner loop will be a function of the control variable in our outer loop. Keeping the logic in mind, our outer loop will run five times. 1 star in the first line, 2 stars in the second line and so on. It prints the increasing number of stars in subsequent lines. So, keeping all of this in mind, let’s try to code the patterns now. ![]() Also, there is the involvement of spaces and tabs to produce the desired pattern.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |