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.
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.
You can also watch the video on YouTube here.
Python Program to print a Floyd’s Triangle.
- Read an input integer for asking the range of the triangle using
- 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 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 triangle with numbers and print * for printing triangle with *’s
__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")
Floyd’s Triangle Pattern – Code Visualization
Machine Learning everywhere! So I strongly suggest you to take the course below.
Course: Machine Learning Adv: Support Vector Machines (SVM) Python