Floyd's triangle in C
What is Floyd's triangle?
A right-angled triangle with successive natural integers is known as Floyd's triangle. The number in Floyd's triangle starts with 1 in the top left corner and continues to fill the defined rows with numbers. IN this blog we will further work on the implementation of Floyd's triangle in c.
Example
If we have to print 6 rows of floyd's triangle in c it will be in the following way:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
Explanation
Print the normal counting of numbers and in each row, print a count of a number equal to the row number for example, in row 1, print 1 element, then in row 2, print 2 elements, and so on. Below we have discussed the C Program to Create Floyd's Triangle.
You can also read about dynamic array in c.
Algorithm
- C Program to Create Floyd's Triangle first Create the variables for holding row number and column number as i and j. Then further take a number to display the rows as 'row' and set the variable count to 1 which will print the elements.
- Now Use the concept of nested loops.
- The iteration of the outer for loop begins with I = 1 and continues up to total rows.
- Iteration of the inner for loop begins at j = 1 and ends at (j=i).
- Print the count values.
- Increase count by one or make count = count + 1.
- After each iteration of the inner for loop, jump to a new line.
- Stop floyd's triangle in c
Also Read, Binary to Hex Converter
Code
// Implementation of floyd's triangle in c
// C Program to Create Floyd's Triangle
#include <stdio.h>
int main()
{
int rows, i, j;
int count = 1;
printf( " Enter the number of rows: \n");
scanf( "%d", &rows);
printf( "Floyd's triangle in c\n");
// using the nested loop to print floyd's triangle in c
for (i = 1; i <= rows; i++)
{
// inner loop check j should be less than equal to given row number and print the data.
for (j = 1; j <= i; j++)
{
// print the number
printf(" %d", count);
count++;
}
printf( "\n");
}
// C Program to Create Floyd's Triangle completed
return 0;
}
Output:
Output of C Program to Create Floyd's Triangle
Enter the number of rows: 6
Floyd's triangle in c
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
You can practice by yourself with the help of online c compiler.
Time Complexity
O(row * row)
The time complexity to print the entire floyd's triangle in c is O(row * row), where 'row' is the total number of rows in the Floyd's triangle. Since we are using the nested loop in the C Program to Create Floyd's Triangle it will cost quadratic time complexity.
Space Complexity
O(1)
The space complexity to print the entire floyd's triangle in c is O(1), Since we have not used any extra data structure to store the elements and neither there are recursive calls so the space complexity for the C Program to Create Floyd's Triangle is constant.
FAQs
- What is a nested loop?
Ans: A nested loop is an inner loop inside the body of an outer loop. The inner loop is triggered by the first pass of the outer loop, which executes to completion. The inner loop is then triggered by the outer loop's second pass. This continues until the outer loop is completed. This process would be disrupted by a break in either the inner or outer loop. - Why is this triangle pattern given a name as Floyd's triangle?
Ans: It was named after a computer scientist Robert w. Floyd. - What is the most observing property of Floyd's triangle?
Ans: By the index of each row, each number in the triangle is less than the number below it. - What is the use of printf and scanf in C?
Ans: We have used printf to print the elements of Floyd's triangle in C printf is used to print a statement in C and scanf is used to taking the user input in C.scanf in the implementation is used to taking the user input for the number of rows. Here the number of rows will signify the number of rows in Floyd's triangle in C. - What is the real life application of Floyd's triangle?
Ans:To a mathematician, it isn't really relevant. Its primary purpose is to serve as a beginner's computer programming exercise. Aside from that, it's a good example of what's known as "triangular numbers."
Key Takeaways
In this article, we have extensively discussed the solution to print floyd's triangle in c along with its time and space complexity.
Until then, All the best for your future endeavors, and Keep Coding. "We hope that this blog has helped you enhance your knowledge regarding this problem, and if you would like to learn more, check out our articles on CodeStudio. Do upvote our blog to help other ninjas grow. Happy Coding!