3 ways in C# to print Fibonacci triangle

C# program to print Fibonacci triangle:

In this post, we will learn how to print a Fibonacci triangle in C#. A triangle is called a Fibonacci triangle if each row of the triangle is the Fibonacci series.

In Fibonacci series, each digit is equal to the sum of preceding two numbers. The first two numbers of this series are 0 and 1. The first ten numbers of the Fibonacci series are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34. As you can see here, each number in the series is equal to the sum of previous two numbers.

Each row of Fibonacci triangle is Fibonacci series. For example,

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

This is a Fibonacci triangle of height 5.

Our program will take the height as an input from the user and print the triangle. Let’s learn the algorithm before we start to write the program.

Algorithm to print a Fibonacci triangle:

We will use the below algorithm to print a Fibonacci triangle of a given height:

  1. Take the height of the triangle as an input from the user. Store it in a variable.
  2. Run two loops. The outer loop will be used to point to each row of the triangle and the inner loop will be used to print the Fibonacci series i.e. row items.
  3. The outer loop will run for height number of times. The inner loop will run for variable amount of time. For the first row, it will print only one value, for the second row it will print two values, three values for the third row etc.
  4. The inner loop will print the Fibonacci series. For the first number, it will print 0, for the second number it will print 1 and for all other numbers it will print the sum of previous two numbers.

Let’s write down the program using this algorithm.

Method 1: C# program by using a for loop:

Let’s write down the C# program:

namespace Program
{

    class Program
    {
        static void Main(string[] args)
        {
            int Height;
            Console.WriteLine("Enter the height of the triangle: ");

            Height = int.Parse(Console.ReadLine());

            int First, Second, Current;

            for (int i = 0; i < Height; i++)
            {
                First = 0; Second = 1;

                for (int j = 0; j <= i; j++)
                {
                    if (j == 0)
                        Console.Write(First + "\t");
                    else if (j == 1)
                        Console.Write(Second + "\t");
                    else
                    {
                        Current = First + Second;
                        Console.Write(Current + "\t");

                        First = Second;
                        Second = Current;
                    }

                }
                Console.WriteLine();
            }
        }
    }
}

Here,

  • It is asking the user to enter the height of the triangle. It stores that value in the Height variable.
  • First and Second integer variables are used to store the first and the second value of the fibonacci series. Current is used to store the current value of the series. It is calculated by adding the previous two numbers of the series.
  • The outer for loop runs from i = 0 to i = Height - 1. The inner for loop runs from j = 0 to j = i. The outer for loop is used to point to the rows and the inner for loop to print the values.
  • The values of First and Second are reset before it starts to print any row value.
  • It calculates the current value by adding the previous two numbers and prints that value.
  • At the end of each iteration of the inner for loop, it prints a new line to move to the next line.

If you run this program, 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

Method 2: C# program by using a while loop:

We can also use a while loop to write the same program. Let me show you how it works:

namespace Program
{

    class Program
    {
        static void Main(string[] args)
        {
            int Height;
            Console.WriteLine("Enter the height of the triangle: ");

            Height = int.Parse(Console.ReadLine());

            int First, Second, Current, i = 0, j;

            while (i < Height)
            {
                First = 0; Second = 1;
                j = 0;

                while (j <= i)
                {
                    if (j == 0)
                        Console.Write(First + "\t");
                    else if (j == 1)
                        Console.Write(Second + "\t");
                    else
                    {
                        Current = First + Second;
                        Console.Write(Current + "\t");

                        First = Second;
                        Second = Current;
                    }
                    j++;
                }
                Console.WriteLine();
                i++;
            }
        }
    }
}

You can see here that the for loops are replaced by while loops. The variable used in the loop condition is initialized before the loop starts and at the end of each iteration, it is incremented. The body of the loops are similar to the previous example.

If you run this program, it will print similar results.

Method 3: C# program with a different function:

Let’s use a different function to print the Fibonacci triangle. The advantage of this approach is that we can use the function from anywhere we want. This function will take the height of the triangle as the parameter and it will print the Fibonacci triangle.

Below is the complete program:

namespace Program
{

    class Program
    {
        static void PrintFibonacci(int Height)
        {
            int First, Second, Current, i = 0, j;

            while (i < Height)
            {
                First = 0; Second = 1;
                j = 0;

                while (j <= i)
                {
                    if (j == 0)
                        Console.Write(First + "\t");
                    else if (j == 1)
                        Console.Write(Second + "\t");
                    else
                    {
                        Current = First + Second;
                        Console.Write(Current + "\t");

                        First = Second;
                        Second = Current;
                    }
                    j++;
                }
                Console.WriteLine();
                i++;
            }
        }

        static void Main(string[] args)
        {
            int Height;
            Console.WriteLine("Enter the height of the triangle: ");

            Height = int.Parse(Console.ReadLine());
            PrintFibonacci(Height);

        }
    }
}

In this program, the PrintFibonacci method is used to print the fibonacci triangle. It uses while loops to print the triangle. It takes the height of the triangle as its argument and prints the triangle of that height.

C# fibonacci triangle example

You might also like: