Python program to print Floyd’s Triangle (3 Ways)
In this program, we will learn how to print Floyd’s Triangle using the Python programming language.
What is Floyd’s Triangle?
We discuss three ways to write code for it.
- Using for Loop
- Using while Loop
- Using Recursion
Python programming code to print Floyd’s Triangle
Using for Loop
Code:-
rows = int(input("Enter the Number of Rows: ")) num = 1 print("Floyd's Triangle") for i in range(1, rows + 1): for j in range(1, i + 1): print(num, end = ' ') num = num + 1 print()
Using while Loop
Code:-
rows = int(input("Enter the Number of Rows: ")) num = 1 i = 1 print("Floyd's Triangle") while(i <= rows): j = 1 while(j <= i): print(num, end = ' ') num = num + 1 j = j + 1 i = i + 1 print()
Using Recursion
Code:-
def printFloydTriangle(rows): if(rows <= 0): return for i in range(1, printFloydTriangle.col+1): print(printFloydTriangle.num, end=" ") printFloydTriangle.num += 1 print() printFloydTriangle.col += 1 rows -= 1 printFloydTriangle(rows) printFloydTriangle.col = 1 printFloydTriangle.num = 1 rows = int(input("Enter the Number of Rows: ")) print("Floyd's Triangle") printFloydTriangle(rows)
Output:-
Floyd’s Triangle
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
You can learn about many other Python Programs Here.
Best Books for learning Python with Data Structure, Algorithms, Machine learning and Data Science.
Amit Rawat
Latest posts by Amit Rawat (see all)
- Python Program to Print the Fibonacci Sequence (2 ways) - April 7, 2020
- Python Program to Display or Print Prime Numbers Between a Range or an Interval - June 18, 2019
- Python Program To Print Pascal’s Triangle (2 Ways) - June 17, 2019