Write a C# program to find Fibonacci Series

How to write C# program to find the Fibonacci series. An example of a C# program for beginners. Step by step C# code to find the Fibonacci series.

In this example, We will see how to find the Fibonacci series in c#. Before directly dive into code lets understand the problem what is the Fibonacci series.

What is the Fibonacci Series?

A series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers, the numbers that precedes the series are 0 and 1. The next number is found by adding up the two numbers number that come before it. Example- Fibonacci series is 0, 1, 1, 2, 3, 5, 8, 13 and so on as you can see in the below picture.

Steps to find a Fibonacci series

  • First, two numbers always will be (0,1)
  • Now find the sum of the two preceding numbers (0+1=1), (1+1 = 2), (2+1 = 3) and so on.

Now Let’s understand how to find the Fibonacci series in C#

Algorithm to find the Fibonacci series in C#.

  • Define variables (int num1 = 0, num2 = 1, num3, i and n)
  • Read a number to set the limit of series in n.
  • Execute the loop until the input limit reach.
  • Print the sum of two predefined numbers(0+1 = 1).
  • Swap the sum (num1 = num2, num2 = num3 )

C# program to find Fibonacci Series by iteration method

using System;
 
namespace CSharpProgram
{
    class FindFibonacciSeries
    {
        static void Main(string[] args)
        {
            int num1 = 0, num2 = 1, num3, i, n;
            Console.Write("Please enter the Length of the Fibonacci Series: ");
            n = int.Parse(Console.ReadLine());
            //Start loop from 2 and execute till the input n-1.
            for (i = 2; i < n; ++i)    
            {
                num3 = num1 + num2;
                Console.Write(num3 + " ");
                num1 = num2;
                num2 = num3;
            }
            
            Console.ReadLine();
        }
    }
}

Output screen:

Here we are storing the first and the second number of Fibonacci series in the previous number and previous number(these are two variables) and also we are using the current number to store the Fibonacci number.

C# program to find the Fibonacci series using Array

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Program
{
public static int[] Fibonacci(int number)
{
int[] a = new int[number];
a[0] = 0;
a[1] = 1;
for (int i = 2; i < number; i++)
{
a[i] = a[i - 2] + a[i - 1];
}
return a;
}
public static void Main(string[] args)
{
var b = Fibonacci(10);
foreach (var elements in b)
{
Console.WriteLine(elements);
}
}
}

Here what we are doing is taking only a one variable a as array data type and then setting a number which we want to get a Fibonacci series as a size of array.

And then at position of a[0] we are setting 0 number and at the position of a[1] we are setting 1 number then the for loop will work whose Initialize number i is set equal to 2 and the loop will run till the i does not reach to the number we have given and then putting a result in a[i] by adding both numbers as a[i-2]  and a[i-1] positions numbers.

C# program to find the Nth Fibonacci number

How to write a C# program to find the Nth Fibonacci number ? Here is the another Example of Program in c# to solve this problem.

using System;
 
namespace CSharpProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1 = 0, num2 = 1, num3 = 0, i, number;
            Console.Write("Please enter the Nth number of the Fibonacci Series: ");
            number = int.Parse(Console.ReadLine());
            for (i = 2; i <= number; ++i)
            {
                num3 = num1 + num2;
                num1 = num2;
                num2 = num3;
            }
            Console.Write(num3 + " ");
            Console.ReadLine();
        }
        
    }
}

Output screen:

In this output you can see that it is not displaying all the series number it only gives an output of the last number we get in the Fibonacci series of our Nth number.

The iteration method is always preferred because it has a faster approach to solve this kind of problem.

So these are some different methods and programs to find the Fibonacci series in C#.