Open In App

C Program To Print Reverse Floyd’s Pattern

Improve
Improve
Like Article
Like
Save
Share
Report

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




// C program to demonstrate reversing
// of floyd's triangle
#include <stdio.h>
void Reverse_Floyd(int row)
{
    // Calculating the maximum value
    int max_val = row * (row + 1) / 2;
 
    // Initializing num with the max value
    int num = max_val;
 
    // The outer loop maintains,
    // the number of rows.
    for (int i = row; i >= 1; i--) {
 
        // The inner loop maintains,
        // the number of column.
        for (int j = 1; j <= i; j++) {
 
            // To print the numbers.
            printf("%d ", num);
 
            // To keep decreasing the value
            // of numbers.
            num--;
        }
 
        // To proceed to next line.
        printf("\n");
    }
}
int main()
{
    int row = 5; // The number of Rows to be printed.
 
    // Calling the Function.
    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.



Last Updated : 26 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads