Print a Floyd’s Triangle Pattern in Python

Avinash Nethala
programminginpython.com
2 min readOct 21, 2018

Hello everyone, welcome back to programminginpython.com! I am going to create a new series on pattern programming, I will start with Floyd’s Triangle pattern.

Floyd’s Triangle pattern — programminginpython.com

A Floyd’s Triangle is a right-angled triangle which is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner. It can also be filled with *’s or any characters as we want. Here I will show you two examples one with numbers and one with *’s.

Task

Python Program to print a Floyd’s Triangle.

Approach

  • Read an input integer for asking the range of the triangle using input()
  • Run 2 for loops, one for column looping and other for row looping, in the first loop, loop through the range of the triangle for row looping
  • In the second loop, loop through the value of 1st loop + 1, this is for column looping
  • Now print the index value for printing triangle with numbers and print * for printing triangle with *’s

Program

__author__ = 'Avinash'# Print a Floyd's Triangle# Range of the triangle
size = int(input("Enter the range: \t "))
print("\nFLOYD'S TRIANGLE with numbers: \n")
k = 1
# 2 for loops, one for column looping another for row looping
# i loop for column looping and j loop for row looping
for i in range(1, size + 1):
for j in range(1, i + 1):
print(k, end=" ")
k = k + 1
print()
print("\n")
print("\nFLOYD'S TRIANGLE with *'s: \n")
for i in range(1, size + 1):
for j in range(1, i + 1):
print('*', end=" ")
print()
print("\n")

Output

Floyd’s Triangle pattern — programminginpython.com
Floyd’s Triangle pattern — programminginpython.com

Also feel free to go through the other posts related to GUI programming in python, or the common algorithms implemented in python or all of the posts here.

--

--