What is Floyd's Triangle?
Floyd's Triangle is the right angled triangular array of natural numbers which starts from 1 at top and have n number of rows. The sum of numbers in all n rows is given by n(n2 + 1)/2. Numbers on the left edge of the triangle are called Lazy Caterer's Sequence and the numbers along hypotenuse of triangle are called Triangular numbers. Sequence number of each row is equal to the number of elements in that row. i.e. First row has one number, second row has two and so on.
In this tutorial, you will learn about the algorithm to print Floyd's Triangle of n rows along with flowchart. The coding part is done in both C and Python.
ALGORITHM FOR FLOYD's TRIANGLE
Step 2: Read the number of rows of floyd's Triangle.(say n)
Step 3: Iterate the outer loop i upto n times to display rows.
Step 4: Iterate the inner loop from j to i.
Step 5: Display the number
Step 6: Display newline after each row.
Step 7: Stop
FLOWCHART FOR FLOYD's TRIANGLE
SOURCE CODE TO DISPLAY FLOYD's TRIANGLE OF N ROWS
1) In C
//C Program to display Floyd's Triangle of n Rows
#include<stdio.h>
int main(){
int n,a,b,i,j;
printf("Enter number of rows");
scanf("%d",&n);
a=1;
for(i=0;i<=n;i++){
for (j=0;j<=i;j++){
printf(" %d ",a);
a++;
}
printf("\n");
}
return 0;
}
2) In python
# Python Program to Display floyd's Triangle of n Rows
n=int(input("Enter number of rows"))
a=1
for i in range(n):
for j in range(i+1):
print(a,"",end="")
a+=1
print("\n")
Sample Output
Enter number of rows 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
0 Comments