Open In App

Program to print Reverse Floyd’s triangle

Last Updated : 20 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Floyd’s triangle is a triangle with first natural numbers. Task is to print reverse of Floyd’s triangle.
Examples: 
 

Input : 4
Output :
10 9 8 7
6 5 4 
3 2 
1 

Input : 5
Output :
15 14 13 12 11 
10 9 8 7 
6 5 4 
3 2 
1

 

 

C++




// CPP program to print reverse of
// floyd's triangle
#include <bits/stdc++.h>
using namespace std;
  
void printReverseFloyd(int n)
{
    int curr_val = n * (n + 1) / 2;
    for (int i = n; i >= 1; i--) {
        for (int j = i; j >= 1; j--) {
            cout << setprecision(2);
            cout << curr_val-- << "  ";
        }
  
        cout << endl;
    }
}
  
// Driver's Code
int main()
{
    int n = 7;
    printReverseFloyd(n);
    return 0;
}
  
// this article is contributed by manish kumar rai


Java




// Java program to print reverse of
// floyd's triangle
import java.io.*;
  
class GFG {
    static void printReverseFloyd(int n)
    {
        int curr_val = n * (n + 1) / 2;
        for (int i = n; i >= 1; i--) {
            for (int j = i; j >= 1; j--) {
                System.out.printf("%2d  ", curr_val--);
            }
  
            System.out.println("");
        }
    }
  
    // Driver method
    public static void main(String[] args)
    {
        int n = 7;
        printReverseFloyd(n);
    }
}
// this article is contributed by manish kumar rai


Python3




# Python3 program to print reverse of 
# floyd's triangle
def printReverseFloyd(n):
  
    curr_val = int(n*(n + 1)/2)
    for i in range(n + 1, 1, -1):
        for j in range(i, 1, -1):
            print(curr_val, end ="  ")
            curr_val -= 1
          
        print("")
          
# Driver code
n = 7
printReverseFloyd(n)


C#




// C# program to print reverse 
// of floyd's triangle
using System;
using System.Globalization;
  
class GFG
{
    static void printReverseFloyd(int n)
    {
        int curr_val = n * (n + 1) / 2;
        for (int i = n; i >= 1; i--)
        {
            for (int j = i; j >= 1; j--) 
            {
                Console.Write(curr_val-- + " ");
            }
  
            Console.WriteLine("");
        }
    }
      
    // Driver Code
    public static void Main()
    {
        int n = 7;
        printReverseFloyd(n);
    }
      
}
  
// This code is contributed by Sam007


PHP




<?php
// PHP program to print reverse 
// of floyd's triangle 
function printReverseFloyd($n
    $curr_val = $n * ($n + 1) / 2; 
    for ($i = $n; $i >= 1; $i--) 
    
        for ( $j = $i; $j >= 1; $j--) 
        
            echo $curr_val-- , " "
        
  
        echo " \n"
    
  
// Driver Code 
$n = 7; 
printReverseFloyd($n); 
  
// This code is contributed by ajit 
?>


Javascript




<script>
  
// Javascript program to print reverse of
// floyd's triangle
  
function printReverseFloyd(n)
    {
        let curr_val = n * (n + 1) / 2;
        for (let i = n; i >= 1; i--) {
            for (let j = i; j >= 1; j--) {
                document.write(curr_val-- + " ");
            }
    
            document.write("<br/>");
        }
    }
  
  
// Driver Code
  
          let n = 7;
        printReverseFloyd(n);
      
</script>


Output: 

28  27  26  25  24  23  22  
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) for given input n

Auxiliary space: O(1)



Previous Article
Next Article

Similar Reads

Program to Print Floyd's Triangle
Floyd's triangle is a triangle with first natural numbers. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Recommended PracticeFloyd\'s triangleTry It! Following program prints Floyd's triangle with n lines. C/C++ Code #include &lt;bits/stdc++.h&gt; using namespace std; void printFloydTriangle(int n) { int i, j, val = 1; for (i = 1; i &lt;= n; i++) { for (j =
3 min read
Program to print a Hollow Triangle inside a Triangle
Given a number N(? 8), the task is to print a Hollow Triangle inside a Triangle pattern.Example: Input: N = 9 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Approach: Let i be the index for rows and j be the index for columns. Then: For sides of outer triangle: If the index of column(j) is equals to (N - i + 1) or (
7 min read
How does Floyd's slow and fast pointers approach work?
We have discussed Floyd's fast and slow pointer algorithms in Detect loop in a linked list. The algorithm is to start two pointers, slow and fast from head of linked list. We move slow one node at a time and fast two nodes at a time. If there is a loop, then they will definitely meet. This approach works because of the following facts. 1) When slow
9 min read
Floyd-Rivest Algorithm
The Floyd-Rivest algorithm is a selection algorithm used to find the kth smallest element in an array of distinct elements. It is similar to the QuickSelect algorithm but has a better running time in practice. Like QuickSelect, the algorithm works on the idea of partitioning. After partitioning an array, the partition element ends up in the correct
11 min read
Floyd’s Cycle Finding Algorithm
Floyd's cycle finding algorithm or Hare-Tortoise algorithm is a pointer algorithm that uses only two pointers, moving through the sequence at different speeds. This algorithm is used to find a loop in a linked list. It uses two pointers one moving twice as fast as the other one. The faster one is called the fast pointer and the other one is called
14 min read
Finding shortest path between any two nodes using Floyd Warshall Algorithm
Given a graph and two nodes u and v, the task is to print the shortest path between u and v using the Floyd Warshall algorithm. Examples: Input: u = 1, v = 3 Output: 1 -&gt; 2 -&gt; 3 Explanation: Shortest path from 1 to 3 is through vertex 2 with total cost 3. The first edge is 1 -&gt; 2 with cost 2 and the second edge is 2 -&gt; 3 with cost 1. In
13 min read
Comparison of Dijkstra’s and Floyd–Warshall algorithms
Dijkstra AlgorithmDijkstra’s Algorithm is a Single-Source Shortest Path SSSP algorithm, i.e., given a source vertex it finds the shortest path from the source to all other vertices. The idea is to generate a SPT (shortest path tree) with a given source as a root and with two sets, one set contains vertices included in the shortest-path tree, other
4 min read
Transitive closure of a graph using Floyd Warshall Algorithm
Given a directed graph, determine if a vertex j is reachable from another vertex i for all vertex pairs (i, j) in the given graph. Here reachable means that there is a path from vertex i to j. The reach-ability matrix is called the transitive closure of a graph. For example, consider below graph Transitive closure of above graphs is 1 1 1 1 1 1 1 1
12 min read
Bellman-Ford vs Floyd-Warshall's algorithm: A Comparative Analysis
Bellman-Ford Algorithm:The Bellman-Ford algorithm is a single-source shortest-path algorithm that works by iteratively relaxing edges in the graph until the shortest path to all vertices is found. It is especially useful for graphs with negative edge weights, as it can detect negative cycles and return a suitable error message. Floyd-Warshall Algor
18 min read
Detecting negative cycle using Floyd Warshall
We are given a directed graph. We need compute whether the graph has negative cycle or not. A negative cycle is one in which the overall sum of the cycle comes negative. Negative weights are found in various applications of graphs. For example, instead of paying cost for a path, we may get some advantage if we follow the path. Examples: Input : 4 4
12 min read