P3: Print Patterns
Simple Right-Angled Triangle Program
Let's create a program to print a right-angled triangle of user specified height:
Explanation
In the right-angled triangle program, there are two nested loops:
The outer loop (
row
) iterates over each level (or row) of the triangle.The inner loop (
star
) prints the number of stars corresponding to the current row number.
Each row has a number of stars equal to its row number, creating a right-angled triangle pattern.
This right-angled triangle program is a straightforward and effective way to teach beginners about nested loops, with a clear visual output that makes it easy to understand the concept.
Pyramid printing program
Explanation
The program starts by asking the user to specify the height of the pyramid.
It then uses two nested loops:
The first loop (outer loop) iterates over the number of levels in the pyramid.
The second loop (inner loop) first prints spaces to create the right alignment and then prints the stars to form the pyramid shape.
Each level of the pyramid has an increasing number of stars, forming a pyramid as the loop progresses.
This program is visually engaging and demonstrates the practical use of nested loops, offering a fun way to understand these concepts.
Last updated