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

4.24 K
6 weeks
475
6 weeks
– / –
45 hours
4 K
80 hours
– / –
1 month
– / –
40 hours
– / –
2 months
– / –
1 year
4 K
80 hours

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
Floyd’s Triangle – Definition, Properties and More
Floyd’s Triangle is a right-angled triangular array of natural numbers named after the American computer scientist Robert W. Floyd. Floyd's Triangle is a simple geometric arrangement of numbers. Let us...read 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
Top 10 Pattern Programs in C
Have you ever wondered how intricate patterns can be created with simple loops in C? Pattern programs in C are a fascinating way to learn about nested loops and control...read more

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
Pascal’s Triangle – Definition, Properties, Applications and More
Pascal’s Triangle, named after the French mathematician Blaise Pascal, has roots that stretch much further back in history. The discovery of Pascal’s Triangle wasn’t a single event but rather a...read 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
Star Pattern Programs in C
Pattern programs in C are a type of problem that uses loops to produce different patterns of numbers, stars (*), or other characters. This blog will dive deeper into Star...read more

Learning If Statement in C
Learning If Statement in C
The if statement in C is a control structure that evaluates a condition. If the condition is true, it runs the associated code; if false, the code is ignored. It’s...read more

Learning Loops in C
Learning Loops in C
In C programming, a loop is a control structure that allows a set of instructions to be executed repeatedly based on a specified condition. Loops provide an efficient way to...read more

Addition of Two Numbers in C
Addition of Two Numbers in C
Delve into C programming with a foundational task: adding two numbers. This guide walks you through each step, from declaring variables to getting the output, making it a perfect starting...read more

Number Pattern Programs in C
Number Pattern Programs in C
Pattern programs in C are a type of problem that use nested loops to produce different patterns of numbers, stars (*), or other characters. In this blog, we will dive...read more

Solving Tower of Hanoi Program in C
Solving Tower of Hanoi Program in C
Tower of Hanoi Puzzle is a brainteaser that involves moving a stack of discs between three pegs, adhering to specific constraints. Tower of Hanoi Program in C is used to...read more

Control Statements in C | Meaning and Types
Control Statements in C | Meaning and Types
Control statements in C are used to determine the order in which the instructions within a program are executed. They are of three types: Selection, Iteration & Jump statements. Think...read more

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