Here we will develop a C Program To Print The Reverse Floyd pattern. Given the value of row(number of rows), we need the approach of looping and using Nested Loop to print this pattern.
Input:
row = 5
Output:
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1
Using the concepts of the Nested loop
The approach is to previously calculate the maximum value which is to be present in the pattern and then decrease it using the decrement operator, and then we can specify rows and columns using the concepts of the Nested loop.
Algorithm:
- Take the Input of row value for the Reverse Floyd pattern.
- Then we need to calculate the max value by Max Value = row * (row + 1) / 2 .
- The Outer Loop used to control the rows iterates from row to 1.
- The Inner Loop used to control the column iterates from 1 to the current row.
Example:
C
#include <stdio.h>
void Reverse_Floyd( int row)
{
int max_val = row * (row + 1) / 2;
int num = max_val;
for ( int i = row; i >= 1; i--) {
for ( int j = 1; j <= i; j++) {
printf ( "%d " , num);
num--;
}
printf ( "\n" );
}
}
int main()
{
int row = 5;
Reverse_Floyd(row);
return 0;
}
|
Output:
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1
Time Complexity: O(n2), as a nested loop, is used.
Auxiliary Space: O(1), no extra space is required, so it is a constant.