Python program: How to print a triangle in Python

Python program: How to print a triangle in Python

# Function to print a right-angled triangle
def print_triangle(n):
    for i in range(1, n+1):
        for j in range(i):
            print("*", end=" ")
        print()

# Example usage
print_triangle(5)

------------------------------------------------

Program output:

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

Post a Comment

0 Comments