Print reverse floyd's triangle
Floyd's Triangle is a pattern of numbers that starts with 1 and increments by 1 for each row. Each row has one more number than the previous row. It is named after Robert Floyd, who introduced this pattern. In the reverse Floyd's Triangle, the numbers are printed in a triangular shape, starting from the highest number and decreasing by 1 in each row.
Problem Statement
The task is to write a program that prints the reverse Floyd's Triangle based on the given number of rows. Given an integer representing the number of rows, the program should generate the triangle pattern in reverse order.
Example
Let's consider an example where the number of rows is 5:
Input: 5
Output:
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
In this example, the reverse Floyd's Triangle is printed with 5 rows. The highest number is 15, and each subsequent row decreases by 1 until reaching 1.
Algorithm
To solve this problem, we can use two nested loops. The outer loop iterates through each row, and the inner loop prints the numbers for that row.
1. Start by defining a function, let's call it floyd_triangle
, which takes an integer parameter representing the number of rows.
2. Inside the function, declare three integer variables: i
for the outer loop, j
for the inner loop, and counter
to keep track of the numbers to be printed.
3. Initialize counter
to the total number of elements in the triangle, which is calculated as (row * (row + 1)) / 2
.
4. Use the outer loop to iterate from 0 to row - 1
. This will represent each row of the triangle.
5. Inside the outer loop, use the inner loop to iterate from i
to row - 1
. This will ensure that the numbers are printed in the reverse order.
6. Inside the inner loop, print the current value of counter
followed by a tab character ("\t"). Then decrement counter
by 1.
7. After the inner loop, print a newline character ("\n") to move to the next row.
8. After the outer loop, print two newline characters to add some spacing before the next test case.
9. Finally, in the main
function, call the floyd_triangle
function with different test cases to see the output.
Pseudocode
function floyd_triangle(row):
counter = (row * (row + 1)) / 2
for i = 0 to row - 1:
for j = i to row - 1:
print counter, "\t"
counter = counter - 1
print "\n"
print "\n\n"
function main():
floyd_triangle(7)
floyd_triangle(5)
main()
Code Solution
Here given code implementation process.
//C Program
//Print reverse floyd's triangle
#include <stdio.h>
//Print floyd triangle of given rows
void floyd_triangle(int row)
{
printf("ROW SIZE : %d\n\n", row);
//declare loop control variable
int i = 0;
int j = 0;
//counter variable is used to display triangle values
int counter = (row * (row + 1)) / 2;
for (i = 0; i < row; ++i)
{
for (j = i; j < row; j++)
{
printf("%d\t", counter);
counter--;
}
printf("\n");
}
printf("\n\n");
}
int main()
{
//Test Case
floyd_triangle(7);
floyd_triangle(5);
return 0;
}
Output
ROW SIZE : 7
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
ROW SIZE : 5
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1
/*
Java Program
Print reverse floyd's triangle
*/
class MyPattern
{
//Print floyd triangle of given rows
public void floyd_triangle(int row)
{
System.out.print("ROW SIZE : " + row + "\n\n");
//declare loop control variable
int i = 0;
int j = 0;
//counter variable is used to display triangle values
int counter = (row * (row + 1)) / 2;
for (i = 0; i < row; ++i)
{
for (j = i; j < row; j++)
{
System.out.print(counter + "\t");
counter--;
}
System.out.print("\n");
}
System.out.print("\n\n");
}
public static void main(String[] args)
{
MyPattern obj = new MyPattern();
//Simple test
obj.floyd_triangle(7);
obj.floyd_triangle(5);
}
}
Output
ROW SIZE : 7
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
ROW SIZE : 5
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1
/*
C++ Program
Print reverse floyd's triangle
*/
#include<iostream>
using namespace std;
class MyPattern
{
public:
//Print floyd triangle of given rows
void floyd_triangle(int row)
{
cout << "ROW SIZE : " << row << "\n\n";
//declare loop control variable
int i = 0;
int j = 0;
//counter variable is used to display triangle values
int counter = ((row + 1) * row) / 2;
for (i = 0; i < row; ++i)
{
for (j = i; j < row; j++)
{
cout << counter << "\t";
counter--;
}
cout << "\n";
}
cout << "\n\n";
}
};
int main()
{
MyPattern obj = MyPattern();
//Simple test
obj.floyd_triangle(7);
obj.floyd_triangle(5);
return 0;
}
Output
ROW SIZE : 7
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
ROW SIZE : 5
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1
/*
C# Program
Print reverse floyd's triangle
*/
using System;
class MyPattern
{
//Print floyd triangle of given rows
public void floyd_triangle(int row)
{
Console.Write("ROW SIZE : " + row + "\n\n");
//declare loop control variable
int i = 0;
int j = 0;
//counter variable is used to display triangle values
int counter = ((row + 1) * row) / 2;
for (i = 0; i < row; i++)
{
for (j = i; j < row; j++)
{
Console.Write(counter + "\t");
counter--;
}
Console.Write("\n");
}
Console.Write("\n\n");
}
public static void Main(String[] args)
{
MyPattern obj = new MyPattern();
//Simple test
obj.floyd_triangle(7);
obj.floyd_triangle(5);
}
}
Output
ROW SIZE : 7
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
ROW SIZE : 5
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1
<?php
/*
Php Program
Print reverse floyd's triangle
*/
class MyPattern
{
//Print floyd triangle of given rows
public function floyd_triangle($row)
{
echo("ROW SIZE : ". $row ."\n\n");
//declare loop control variable
$i = 0;
$j = 0;
//counter variable is used to display triangle values
$counter = intval((($row + 1) * $row) / 2);
for ($i = 0; $i < $row; ++$i)
{
for ($j = $i; $j < $row; $j++)
{
echo($counter ."\t");
$counter--;
}
echo("\n");
}
echo("\n\n");
}
}
function main()
{
$obj = new MyPattern();
//Simple test
$obj->floyd_triangle(7);
$obj->floyd_triangle(5);
}
main();
Output
ROW SIZE : 7
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
ROW SIZE : 5
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1
/*
Node Js Program
Print reverse floyd's triangle
*/
class MyPattern
{
//Print floyd triangle of given rows
floyd_triangle(row)
{
process.stdout.write("ROW SIZE : " + row + "\n\n");
//declare loop control variable
var i = 0;
var j = 0;
//counter variable is used to display triangle values
var counter = parseInt(((row + 1) * row) / 2);
for (i = 0; i < row; ++i)
{
for (j = i; j < row; j++)
{
process.stdout.write(counter + "\t");
counter--;
}
process.stdout.write("\n");
}
process.stdout.write("\n\n");
}
}
function main(args)
{
var obj = new MyPattern();
//Simple test
obj.floyd_triangle(7);
obj.floyd_triangle(5);
}
main();
Output
ROW SIZE : 7
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
ROW SIZE : 5
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1
# Python 3 Program
# Print reverse floyd's triangle
class MyPattern :
# Print floyd triangle of given rows
def floyd_triangle(self, row) :
print("ROW SIZE : ", row ,"\n")
# declare loop control variable
i = 0
j = 0
# counter variable is used to display triangle values
counter = int(((row + 1) * row) / 2)
while (i < row) :
j = i
while (j < row) :
print(counter , end = "\t")
counter -= 1
j += 1
print(end = "\n")
i += 1
print("\n")
def main() :
obj = MyPattern()
# Simple test
obj.floyd_triangle(7)
obj.floyd_triangle(5)
if __name__ == "__main__": main()
Output
ROW SIZE : 7
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
ROW SIZE : 5
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1
# Ruby Program
# Print reverse floyd's triangle
class MyPattern
# Print floyd triangle of given rows
def floyd_triangle(row)
print("ROW SIZE : ", row ,"\n\n")
# declare loop control variable
i = 0
j = 0
# counter variable is used to display triangle values
counter = ((row + 1) * row) / 2
while (i < row)
j = i
while (j < row)
print(counter ,"\t")
counter -= 1
j += 1
end
print("\n")
i += 1
end
print("\n\n")
end
end
def main()
obj = MyPattern.new()
# Simple test
obj.floyd_triangle(7)
obj.floyd_triangle(5)
end
main()
Output
ROW SIZE : 7
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
ROW SIZE : 5
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1
/*
Scala Program
Print reverse floyd's triangle
*/
class MyPattern
{
//Print floyd triangle of given rows
def floyd_triangle(row: Int): Unit = {
print("ROW SIZE : " + row + "\n\n");
//declare loop control variable
var i: Int = 0;
var j: Int = 0;
//counter variable is used to display triangle values
var counter: Int = (((row + 1) * row) / 2).toInt;
while (i < row)
{
j = i;
while (j < row)
{
print(""+counter + "\t");
counter -= 1;
j += 1;
}
print("\n");
i += 1;
}
print("\n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyPattern = new MyPattern();
//Simple test
obj.floyd_triangle(7);
obj.floyd_triangle(5);
}
}
Output
ROW SIZE : 7
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
ROW SIZE : 5
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1
/*
Swift Program
Print reverse floyd"s triangle
*/
class MyPattern
{
//Print floyd triangle of given rows
func floyd_triangle(_ row: Int)
{
print("ROW SIZE : ", row ,"\n", terminator: "\n");
//declare loop control variable
var i: Int = 0;
var j: Int = 0;
//counter variable is used to display triangle values
var counter: Int = ((row + 1) * row) / 2;
while (i < row)
{
j = i;
while (j < row)
{
print(counter , terminator: "\t");
counter -= 1;
j += 1;
}
print(terminator: "\n");
i += 1;
}
print(terminator: "\n\n");
}
}
func main()
{
let obj: MyPattern = MyPattern();
//Simple test
obj.floyd_triangle(7);
obj.floyd_triangle(5);
}
main();
Output
ROW SIZE : 7
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
ROW SIZE : 5
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1
Output Explanation
The output shows the reverse Floyd's Triangle for each test case.
In the first test case with row = 7
, the triangle has 7 rows. The highest number is 28, and each subsequent row decreases by 1. The numbers are printed in reverse order.
The second test case with row = 5
follows the same pattern. The highest number is 15, and each subsequent row decreases by 1 until reaching 1.
Time Complexity
The time complexity of this algorithm is O(n^2), where n is the number of rows. This is because we have two nested loops that iterate from 0 to n-1.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment