Floyd’s Triangle in C

Floyd’s Triangle in C

3 mins read217 Views Comment
Updated on Jan 2, 2024 14:54 IST

Floyd's Triangle is a unique pattern in programming, typically demonstrated in languages like C. It's a right-angled triangular array of natural numbers, where the first row contains one element, the second row contains two elements, and so on, with each row's elements incrementing from the previous row's last number. Let's understand more!

Floyd's Triangle is a mathematical pattern used in computer programming and mathematics. It is a right-angled triangular arrangement of natural numbers, where each row contains one more number than the previous row. The numbers in the triangle are sequentially arranged, starting with 1 at the top. Each subsequent number is placed below the last number of the previous row, creating a pattern that looks like a triangle. Floyd's Triangle is a useful tool for teaching nested loops and array usage in programming and exploring number patterns in mathematics.

Table of Contents

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

– / –
40 hours
4 K
2 months
– / –
6 months
– / –
1 month
3.5 K
3 months
15 K
2 months
– / –
1 month
– / –
2 months
– / –
50 hours
– / –
4 months

What is a Floyd’s Triangle? 

Floyd’s Triangle is a triangular array of natural numbers named after Robert Floyd, who was an American computer scientist. It is a right-angled triangular array of natural numbers, where the first row contains a single number 1, the second row contains two numbers 2 and 3, the third row contains three numbers 4, 5 and 6, and so on. 

2023_03_image-3.jpg

The pattern of Floyd’s Triangle can be easily constructed using a few common methods in the C programming language. Let’s look at them below. 

Floyd’s Triangle – Definition, Properties and More

Constructing a Floyd’s Triangle in C 

Method 1: Using 1D Array 

This method uses a one-dimensional array to store the numbers in Floyd’s Triangle. We iterate through each row of the triangle, filling the array with the numbers in that row. Then, we print out the array elements in the appropriate format to create Floyd’s Triangle.  

Here’s an example code


 
#include <stdio.h>
int main() {
int rows, i, j, k = 0, number = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
int triangle[rows*(rows+1)/2]; // one-dimensional array to store triangle
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
triangle[k] = number;
number++;
k++;
}
}
k = 0;
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
printf("%d ", triangle[k]);
k++;
}
printf("\n");
}
return 0;
}
Copy code

Output

2023_03_image-6.jpg

Top 10 Pattern Programs in C

Method 2: Using Nested Loops 

Here is how you can use nested loops to construct a Floyd’s Triangle in C


 
#include <stdio.h>
int main() {
int rows, number = 1, i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
printf("%d ", number);
number++;
}
printf("\n");
}
return 0;
}
Copy code

Output 

2023_03_image-9.jpg

Pascal’s Triangle – Definition, Properties, Applications and More

Method 3: Using Recursion 

Here is how you can use recursion to construct a Floyd’s Triangle in C


 
#include <stdio.h>
void print_floyd(int currentRow, int numberOfRows, int numberToPrint);
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
print_floyd(1, n, 1);
return 0;
}
void print_floyd(int currentRow, int numberOfRows, int numberToPrint) {
int c;
if (currentRow > numberOfRows) // Base case
return;
for (c = 1; c <= currentRow; c++)
printf("%d ", numberToPrint++);
printf("\n");
print_floyd(++currentRow, numberOfRows, numberToPrint);
}
Copy code

Output

2023_03_image-10.jpg

This program defines a print_floyd function that prints Floyd's Triangle using recursion. It takes the current row, total number of rows, and the next number to print as arguments. The base case for recursion is when the current row exceeds the total number of rows.

Star Pattern Programs in C

Learning If Statement in C

Learning Loops in C

Addition of Two Numbers in C

Number Pattern Programs in C

Solving Tower of Hanoi Program in C

Control Statements in C | Meaning and Types

Conclusion

Thus, C programmers can create Floyd's Triangle using nested loops or even recursion. It serves as a valuable teaching tool for understanding loop structures and number patterns in programming.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio