Print Floyd’s Triangle in java

In this tutorial we will take a quick look at the Floyd’s triangle using the java language. The Floyd’s triangle (named after Robert Floyd) algorithm is a right-angled triangular array of natural numbers. It is defined by filling the rows of the triangle with consecutive numbers, starting with the number one in the top left corner.

e.g: print floyds triangle for input 4

1

2        3

4          5          6

7           8          9      10

 

</pre>
<pre>
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
* Java program to print floyd's triangle
* Created by SANI KAMAL on 05-May-17.
*/
public class FloydsTriangle {
public static void main(String[] args) {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Enter of rows of Floyd's Triangle:");
int num=Integer.parseInt(br.readLine());
int number=1;
for (int i = 0; i <num ; i++) {
for (int j = 0; j <=i ; j++) {
System.out.print(number+" ");
number++;

}
System.out.println();

}

}catch (Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}

}
}
Advertisements

One thought on “Print Floyd’s Triangle in java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s