Floyd’s Triangle in C
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!
Table of Contents
Best-suited C / C++ courses for you
Learn C / C++ with these high-rated online courses
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.
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.
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; }
Output
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;}
Output
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);}
Output
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.
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.
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