Home » Floyds Triangle Pattern in Python

Floyds Triangle Pattern in Python

Floyds Triangle Pattern in Python

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
Floyd’s Triangle Pattern

A Floyd’s Triangle is a right-angled triangle that 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.

You can also watch the video on YouTube here.

Program on GitHub

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 the other for row looping, in the first loop, loop through the range of the triangle for column looping
  • In the second loop, loop through the value of 1st loop + 1, this is for row looping
  • Now print the index value for printing the triangle with numbers and print * for printing the triangle with *’s

Ad:
The Complete Python Bootcamp From Zero to Hero in Python – Enroll Now.
Udemy

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")

 

Program on GitHub

Output

Floyd's Triangle Pattern
Floyd’s Triangle Pattern

Floyd’s Triangle Pattern – Code Visualization

Program on GitHub

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

Course Suggestion

Machine Learning is everywhere! So I strongly suggest you to take the course below.
Course: Machine Learning Adv: Support Vector Machines (SVM) Python

 

Online Python Compiler

Leave a Reply

Your email address will not be published. Required fields are marked *