# 3 ways in Java to print a Fibonacci triangle

## Java program to print a Fibonacci triangle:

This post will show you how to print a Fibonacci triangle in Java. Each row of a Fibonacci triangle is a Fibonacci series. Each digit in Fibonacci series is equal to the sum of preceding two numbers. The first two numbers of this series are 0 and 1.

The first 10 digits of the Fibonacci series are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34… etc.

We will write a program that will take the height of the triangle as an input from the user and it will print a Fibonacci triangle of that height.

We will learn different ways to print the triangle.

### Algorithm to print a Fibonacci triangle:

The following algorithm will be used to print the Fibonacci triangle:

• Take the height of the triangle as an input from the user.
• Run two loops. The outer loop will point to each row of the triangle and the inner loop will print the body of the triangle.
• The outer loop will run for height number of times.
• On each iteration of the outer loop, the inner loop will run for different number of times. For the first row, it will run 1 time, for the second row, it will run 2 times etc.
• It will print the Fibonacci series in the inner loop. For the first two numbers, it will print 0 and 1. For the other numbers, it will keep calculating the value by adding the previous two numbers.
• At the end of each iteration of the outer loop, it will print a new line to move to the next line of the triangle.

### Example 1: Java program to print a Fibonacci triangle by using for loops:

Let’s write down the program that uses for loops to print the Fibonacci triangle in Java:

``````import java.util.Scanner;

class Main {

public static void main(String[] args) {
int height;
Scanner sc = new Scanner(System.in);

System.out.println("Enter the height of the triangle: ");
height = sc.nextInt();
System.out.println();

for (int i = 1; i <= height; i++) {
int first = 0, second = 1, temp;

for (int j = 1; j <= i; j++) {
if (j == 1)
System.out.print("0");
else if (j == 2)
System.out.print("1");
else {
System.out.print(first + second);
temp = first + second;
first = second;
second = temp;
}
System.out.print("\t");
}
System.out.println();
}
}
}``````
• This program asks the user to enter the height of the triangle, read and stores it in the variable height.
• The outer for loop runs for height number of times and the inner for loop runs for i number of times. i is used in the outer for loop and it starts from 1. So, for the first line, the inner for loop will run only once, for the second line it run only twice etc.
• The inner for loop prints the fibonacci series, i.e. the body of the triangle. It prints 0 and 1 for the first two values of the series. Else, it prints the sum of previous two values. The values of first and second are updated accordingly.
• It prints a tab space, \t between each number and adds a newline once a line of the series is completed.

It will print output as like below:

``````Enter the height of the triangle:
5

0
0	1
0	1	1
0	1	1	2
0	1	1	2	3

Enter the height of the triangle:
10

0
0	1
0	1	1
0	1	1	2
0	1	1	2	3
0	1	1	2	3	5
0	1	1	2	3	5	8
0	1	1	2	3	5	8	13
0	1	1	2	3	5	8	13	21
0	1	1	2	3	5	8	13	21	34	``````

### Example 2: Java program to print a Fibonacci triangle by using while loops:

We can also use while loops to print the same triangle in Java.

``````import java.util.Scanner;

class Main {

public static void main(String[] args) {
int height, i = 1;
Scanner sc = new Scanner(System.in);

System.out.println("Enter the height of the triangle: ");
height = sc.nextInt();
System.out.println();

while (i <= height) {
int first = 0, second = 1, temp, j = 1;

while (j <= i) {
if (j == 1)
System.out.print("0");
else if (j == 2)
System.out.print("1");
else {
System.out.print(first + second);
temp = first + second;
first = second;
second = temp;
}
System.out.print("\t");
j++;
}
System.out.println();
i++;
}
}
}``````

This is almost similar to the above program. The only difference is that i and j are initialized before the loop starts and the values of these variables are incremented at the end of each iteration of the loops.

If you run this program, it will give you similar result.

### Example 3: Java program to print a Fibonacci triangle by using a separate method:

We can always use a separate method to write the logical part. The method will take the height of the triangle as its parameter and print the triangle.

Let’s re-write the program:

``````import java.util.Scanner;

class Main {

private static void printFibonacciTriangle(int height){
for (int i = 1; i <= height; i++) {
int first = 0, second = 1, temp;

for (int j = 1; j <= i; j++) {
if (j == 1)
System.out.print("0");
else if (j == 2)
System.out.print("1");
else {
System.out.print(first + second);
temp = first + second;
first = second;
second = temp;
}
System.out.print("\t");
}
System.out.println();
}
}

public static void main(String[] args) {
int height;
Scanner sc = new Scanner(System.in);

System.out.println("Enter the height of the triangle: ");
height = sc.nextInt();
System.out.println();

printFibonacciTriangle(height);
}
}``````

This program uses printFibonacciTriangle method to print the Fibonacci triangle. This method takes the height of the triangle as the parameter and prints the triangle.

It will give similar output:

``````Enter the height of the triangle:
5

0
0	1
0	1	1
0	1	1	2
0	1	1	2	3	``````