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++
#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;
}
}
int main()
{
int n = 7;
printReverseFloyd(n);
return 0;
}
|
Java
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( "" );
}
}
public static void main(String[] args)
{
int n = 7 ;
printReverseFloyd(n);
}
}
|
Python3
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 ("")
n = 7
printReverseFloyd(n)
|
C#
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( "" );
}
}
public static void Main()
{
int n = 7;
printReverseFloyd(n);
}
}
|
PHP
<?php
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" ;
}
}
$n = 7;
printReverseFloyd( $n );
?>
|
Javascript
<script>
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/>" );
}
}
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)