Skip to content

Print Fibonacci Series For A Given Length Using For Loop

We are going to print Fibonacci Series of the user provided length.

In order to execute the code I’ve created a simple console application to print out the result.

Please find complete code below:

using System;
using System.Text;

namespace PracticeConsole
{
   class Program
   {
      static void Main(string[] args)
      {
         Console.WriteLine("Please enter desired length of the Fibonacci Series and then hit enter:");
         int fibonacciLength = Convert.ToInt32(Console.ReadLine());
         int a = 0, b = 1, c = 0;
         StringBuilder fibonacciSeriesResult = new StringBuilder();
         for (int i = 2; i < fibonacciLength; i++)
         {
            fibonacciSeriesResult = fibonacciSeriesResult.Append(" ").Append(c);
            c = a + b;
            a = b;
            b = c;
         }
         Console.WriteLine(fibonacciSeriesResult);
         Console.ReadLine();
      }
   }
}

Result: