Print reverse floyd's triangle
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
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