Java program to print floyd’s triangle

Java program to print floyd’s triangle

It is common that interviewer asking freshers to write Java program to print numbers or some character such as * in some pattern. These kind of programs are asked to test the knowledge of freshers in usage of nested loops. One such program is printing Floyd’s triangle. In this post we will see Java program to print floyd’s triangle. The floyd’s triangle looks like the below pattern,

floyd's triangle

There are N rows in the pattern and in each ith row there are i number of elements i.e. 1st row has one element, 2nd row has two elements, 3rd row has three elements and so on.

Here is the Java program to print floyd’s triangle using nested loops.

Program to print floyd’s triangle using recursion

If the interviewer asks you to print it to a file instead of the console, you can use the below program.

When you run the above program a file name floydTriange.txt will be created in the D drive. Here is a sample output of running the above program.

floyd's triangle

It is a good programming exercise to build your knowledge on loops especially nested loops. This will also be helpful in learning to create other patterns like Pascal’s triangle etc.

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.

Add Comment

Required fields are marked *. Your email address will not be published.