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
* * * * * * * * * *
1. Floyd’s triangle using for loop
// C program to Demonstrate Floyd's Triangle // Using for loop #include <stdio.h> void floyd( int n)
{ int i, j = 1;
// Condition printing the number of element
for (i = 1; i <= (n * (n + 1)) / 2; i++) {
printf ( "%d " , i);
// condition for row of number of element printing
if (i == (j * (j + 1)) / 2) {
printf ( "\n" );
j++;
}
}
} 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(1)
2. Floyd’s triangle using while loop
// C program to Demonstrate Floyd's Triangle // Using while loop #include <stdio.h> void floyd( int n)
{ int i = 1, j = 1;
// condition for number of element
while (i <= (n * (n + 1)) / 2) {
printf ( "%d " , i);
// condition for what element has to print and
// how many times
if (i == (j * (j + 1)) / 2) {
printf ( "\n" );
j++;
}
i++;
}
} 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(1)
3. Floyd’s triangle using recursion
// 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
4. Reverse Floyd’s triangle using for loop
// C program to Demonstrate Reverse Floyd's // Triangle Using for loop #include <iostream> using namespace std;
void reverse_floyd( int n)
{ // total number of elements
int i = n * (n + 1) / 2;
// condition for printing them
while (i > 0) {
for ( int j = 0; j < n; j++)
printf ( "%d " , i--);
n--;
printf ( "\n" );
}
} int main()
{ reverse_floyd(6);
return 0;
} |
Output:
21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Time Complexity: O(n2)
Auxiliary Space: O(1)
5. Star Floyd’s triangle using for loop
// C program to Demonstrate Star Floyd's // Triangle Using for loop #include <stdio.h> void floyd( int n)
{ int i, j = 1;
for (i = 1; i <= (n * (n + 1)) / 2; i++) {
printf ( "* " );
if (i == (j * (j + 1)) / 2) {
printf ( "\n" );
j++;
}
}
} int main() { floyd(6); }
|
Output:
* * * * * * * * * * * * * * * * * * * * *
Time Complexity: O(n2)
Auxiliary Space: O(1)
6. Alphabets Floyd’s triangle using for loop
// C program to Demonstrate Alphabet Floyd's // Triangle Using for loop #include <stdio.h> void alpha_floyd( int n)
{ int k = 'a' ;
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= i; j++)
printf ( "%c " , k++);
printf ( "\n" );
}
} int main()
{ alpha_floyd(6);
return 0;
} |
Output:
a b c d e f g h i j k l m n o p q r s t u
Time Complexity: O(n2)
Auxiliary Space: O(1)