Floyd’s triangle using recursion
C
// C program to Demonstrate Floyd's Triangle // Using recursion #include <stdio.h> int row = 1, i = 1; void floyd( int n) { // base condition if (n == 0) return ; for ( int j = 1; j <= row; j++) printf ( "%d " , i++); row++; printf ( "\n" ); floyd(n - 1); } int main() { floyd(6); } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Time Complexity: O(n2)
Auxiliary Space: O(n) for call stack, because using recursion
C Program to Print Floyd’s Triangle Pyramid Patterns
Here we will build a C program to print Floyd’s Triangle Pyramid pattern. Floyd’s Triangle is a triangular array of natural numbers where the nth row contains n elements. There are 8 methods to cover all variations of Floyd’s Triangles
- Floyd’s triangle using for loop.
- Floyd’s triangle using while loop.
- Floyd’s triangle using recursion.
- Reverse Floyd’s triangle using for loop.
- Star Floyd’s triangle using for loop.
- Alphabets Floyd’s triangle using for loop.
Floyd’s Triangle of natural numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Floyd’s triangle of alphabets
a b c d e f g h i j
Star Floyd pattern
* * * * * * * * * *