Print Floyd's triangle
In this article, we will explore Floyd's Triangle and how to print it using Java programming language. Floyd's Triangle is a right-angled triangular pattern of numbers named after Robert Floyd. It starts with 1 and each subsequent row contains the next set of natural numbers.

Problem Statement
The problem is to print Floyd's Triangle of a given number of rows. For example, if the input is 7, the output would be:
ROW SIZE : 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
We need to design a program that takes the number of rows as input and prints Floyd's Triangle accordingly.
Approach and Algorithm
To solve this problem, we can use nested loops. The outer loop iterates over the rows, and the inner loop prints the elements in each row. We maintain a counter variable to keep track of the current number to be printed. Here's the algorithm:
- Start with a counter variable set to 1.
- Loop over the rows from 1 to the given input row number.
- For each row, loop over the columns from 1 to the current row number.
- Print the value of the counter variable, followed by a tab or space for formatting.
- Increment the counter variable by 1.
- Print a new line after each row to move to the next line.
- Repeat steps 3-6 for all rows.
The time complexity of this algorithm is O(n^2), where n is the number of rows in Floyd's Triangle.
Code Solution
Here given code implementation process.
// Java Program for
// Print Floyd's Triangle
class MyPattern
{
// Print floyd triangle of given rows
public void floydTriangle(int row)
{
if(row <= 0 )
{
return;
}
// Display number of row
System.out.print("ROW SIZE : " + row + "\n\n");
// Counter variable is used to display triangle values
int counter = 1;
// Outer loop for rows
for (int i = 0; i < row; ++i)
{
for (int j = 0; j <= i; j++)
{
// Print element value
System.out.print(" " + counter + "\t");
// Increase counter value by one
counter++;
}
// Include new line
System.out.print("\n");
}
System.out.print("\n\n");
}
public static void main(String[] args)
{
MyPattern task = new MyPattern();
// Test
task.floydTriangle(7);
task.floydTriangle(5);
}
}
Output
ROW SIZE : 7
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
ROW SIZE : 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
// Include header file
#include <iostream>
using namespace std;
// C++ Program for
// Print Floyd's Triangle
class MyPattern
{
public:
// Print floyd triangle of given rows
void floydTriangle(int row)
{
if (row <= 0)
{
return;
}
// Display number of row
cout << "ROW SIZE : " << row << "\n\n";
// Counter variable is used to display triangle values
int counter = 1;
// Outer loop for rows
for (int i = 0; i < row; ++i)
{
for (int j = 0; j <= i; j++)
{
// Print element value
cout << " " << counter << "\t";
// Increase counter value by one
counter++;
}
// Include new line
cout << "\n";
}
cout << "\n\n";
}
};
int main()
{
MyPattern *task = new MyPattern();
// Test
task->floydTriangle(7);
task->floydTriangle(5);
return 0;
}
Output
ROW SIZE : 7
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
ROW SIZE : 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
// C Program
// Print 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 = 1;
for (i = 0; i < row; ++i)
{
for (j = 0; j <= i; 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
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
ROW SIZE : 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
package main
import "fmt"
// Go Program for
// Print Floyd's Triangle
// Print floyd triangle of given rows
func floydTriangle(row int) {
if row <= 0 {
return
}
// Display number of row
fmt.Print("ROW SIZE : ", row, "\n\n")
// Counter variable is used to display triangle values
var counter int = 1
// Outer loop for rows
for i := 0 ; i < row ; i++ {
for j := 0 ; j <= i ; j++ {
// Print element value
fmt.Print(" ", counter, "\t")
// Increase counter value by one
counter++
}
// Include new line
fmt.Print("\n")
}
fmt.Print("\n\n")
}
func main() {
// Test
floydTriangle(7)
floydTriangle(5)
}
Output
ROW SIZE : 7
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
ROW SIZE : 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
// Include namespace system
using System;
// Csharp Program for
// Print Floyd's Triangle
public class MyPattern
{
// Print floyd triangle of given rows
public void floydTriangle(int row)
{
if (row <= 0)
{
return;
}
// Display number of row
Console.Write("ROW SIZE : " + row + "\n\n");
// Counter variable is used to display triangle values
int counter = 1;
// Outer loop for rows
for (int i = 0; i < row; ++i)
{
for (int j = 0; j <= i; j++)
{
// Print element value
Console.Write(" " + counter + "\t");
// Increase counter value by one
counter++;
}
// Include new line
Console.Write("\n");
}
Console.Write("\n\n");
}
public static void Main(String[] args)
{
MyPattern task = new MyPattern();
// Test
task.floydTriangle(7);
task.floydTriangle(5);
}
}
Output
ROW SIZE : 7
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
ROW SIZE : 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
<?php
// Php Program for
// Print Floyd's Triangle
class MyPattern
{
// Print floyd triangle of given rows
public function floydTriangle($row)
{
if ($row <= 0)
{
return;
}
// Display number of row
print_r("ROW SIZE : ".strval($row).
"\n\n");
// Counter variable is used to display triangle values
$counter = 1;
// Outer loop for rows
for ($i = 0; $i < $row; ++$i)
{
for ($j = 0; $j <= $i; $j++)
{
// Print element value
print_r(" ".strval($counter).
"\t");
// Increase counter value by one
$counter++;
}
// Include new line
print_r("\n");
}
print_r("\n\n");
}
public static
function main($args)
{
$task = new MyPattern();
// Test
$task->floydTriangle(7);
$task->floydTriangle(5);
}
}
MyPattern::main(array());
Output
ROW SIZE : 7
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
ROW SIZE : 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
// Node JS Program for
// Print Floyd's Triangle
class MyPattern
{
// Print floyd triangle of given rows
floydTriangle(row)
{
if (row <= 0)
{
return;
}
// Display number of row
process.stdout.write("ROW SIZE : " + row + "\n\n");
// Counter variable is used to display triangle values
var counter = 1;
// Outer loop for rows
for (var i = 0; i < row; ++i)
{
for (var j = 0; j <= i; j++)
{
// Print element value
process.stdout.write(" " + counter + "\t");
// Increase counter value by one
counter++;
}
// Include new line
process.stdout.write("\n");
}
process.stdout.write("\n\n");
}
}
function main()
{
var task = new MyPattern();
// Test
task.floydTriangle(7);
task.floydTriangle(5);
}
main();
Output
ROW SIZE : 7
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
ROW SIZE : 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
# Python 3 Program for
# Print Floyd's Triangle
class MyPattern :
# Print floyd triangle of given rows
def floydTriangle(self, row) :
if (row <= 0) :
return
# Display number of row
print("ROW SIZE : ", row ,"\n")
# Counter variable is used to display triangle values
counter = 1
i = 0
# Outer loop for rows
while (i < row) :
j = 0
while (j <= i) :
# Print element value
print(" ", counter ,"\t", end = "")
# Increase counter value by one
counter += 1
j += 1
# Include new line
print(end = "\n")
i += 1
print("\n")
def main() :
task = MyPattern()
# Test
task.floydTriangle(7)
task.floydTriangle(5)
if __name__ == "__main__": main()
Output
ROW SIZE : 7
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
ROW SIZE : 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
# Ruby Program for
# Print Floyd's Triangle
class MyPattern
# Print floyd triangle of given rows
def floydTriangle(row)
if (row <= 0)
return
end
# Display number of row
print("ROW SIZE : ", row ,"\n\n")
# Counter variable is used to display triangle values
counter = 1
i = 0
# Outer loop for rows
while (i < row)
j = 0
while (j <= i)
# Print element value
print(" ", counter ,"\t")
# Increase counter value by one
counter += 1
j += 1
end
# Include new line
print("\n")
i += 1
end
print("\n\n")
end
end
def main()
task = MyPattern.new()
# Test
task.floydTriangle(7)
task.floydTriangle(5)
end
main()
Output
ROW SIZE : 7
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
ROW SIZE : 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
// Scala Program for
// Print Floyd's Triangle
class MyPattern()
{
// Print floyd triangle of given rows
def floydTriangle(row: Int): Unit = {
if (row <= 0)
{
return;
}
// Display number of row
print("ROW SIZE : " + row + "\n\n");
// Counter variable is used to display triangle values
var counter: Int = 1;
var i: Int = 0;
// Outer loop for rows
while (i < row)
{
var j: Int = 0;
while (j <= i)
{
// Print element value
print(" " + counter + "\t");
// Increase counter value by one
counter += 1;
j += 1;
}
// Include new line
print("\n");
i += 1;
}
print("\n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: MyPattern = new MyPattern();
// Test
task.floydTriangle(7);
task.floydTriangle(5);
}
}
Output
ROW SIZE : 7
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
ROW SIZE : 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
// Swift 4 Program for
// Print Floyd's Triangle
class MyPattern
{
// Print floyd triangle of given rows
func floydTriangle(_ row: Int)
{
if (row <= 0)
{
return;
}
// Display number of row
print("ROW SIZE : ", row ,"\n");
// Counter variable is used to display triangle values
var counter: Int = 1;
var i: Int = 0;
// Outer loop for rows
while (i < row)
{
var j: Int = 0;
while (j <= i)
{
// Print element value
print(" ", counter ,"\t", terminator: "");
// Increase counter value by one
counter += 1;
j += 1;
}
// Include new line
print(terminator: "\n");
i += 1;
}
print("\n");
}
}
func main()
{
let task: MyPattern = MyPattern();
// Test
task.floydTriangle(7);
task.floydTriangle(5);
}
main();
Output
ROW SIZE : 7
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
ROW SIZE : 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
// Kotlin Program for
// Print Floyd's Triangle
class MyPattern
{
// Print floyd triangle of given rows
fun floydTriangle(row: Int): Unit
{
if (row <= 0)
{
return;
}
// Display number of row
print("ROW SIZE : " + row + "\n\n");
// Counter variable is used to display triangle values
var counter: Int = 1;
var i: Int = 0;
// Outer loop for rows
while (i < row)
{
var j: Int = 0;
while (j <= i)
{
// Print element value
print(" " + counter + "\t");
// Increase counter value by one
counter += 1;
j += 1;
}
// Include new line
print("\n");
i += 1;
}
print("\n\n");
}
}
fun main(args: Array < String > ): Unit
{
val task: MyPattern = MyPattern();
// Test
task.floydTriangle(7);
task.floydTriangle(5);
}
Output
ROW SIZE : 7
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
ROW SIZE : 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Finally
In this article, we have learned about Floyd's Triangle and how to print it using Java. We discussed the problem statement and provided a step-by-step algorithm to solve it. The Java code implementation was also explained, along with the expected output. Floyd's Triangle is a fascinating pattern that can be generated using simple loops. It helps in understanding nested loops and pattern printing in programming.
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