In Fibonacci Series, we start number sequence from 0,1 and keep adding last 2 numbers until n times. So basically Fibonacci series numbers are as below:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89.....
The next number is found by adding up the two numbers before it. So in this article, I have mentioned how we can create Fibonacci series in C# using various possible ways in C# including a recursive one.
Fibonacci series is a series of natural numbers where next number is equivalent to the sum of the previous two numbers i.e. fn = fn-1 + fn-2.In Fibonacci sequence, each item is the sum of the previous two. So, you wrote a recursive algorithm, for example, a recursive function example for up to 5
fibonacci(5) = fibonacci(4) + fibonacci(3)
fibonacci(3) = fibonacci(2) + fibonacci(1)
fibonacci(4) = fibonacci(3) + fibonacci(2)
fibonacci(2) = fibonacci(1) + fibonacci(0)
The first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, and each subsequent number is the sum of the previous two numbers.
Using Recursion
In the Recursive way to print the Fibonacci series in C#, we have created a function named "Fibonacci_Recursive", which is called by itself for each counter, until we reach the required length, entered by the user.
using System;
namespace Fibonacci
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Length of Fibonacci Series: ");
var len = Convert.ToInt32(Console.ReadLine());
//call recursive function
Fibonacci_Recursive(0, 1, 1, len);
Console.WriteLine();
}
private static void Fibonacci_Recursive(int a, int b, int counter, int len)
{
if (counter <= len)
{
Console.Write("{0} ", a);
//call recursive function
Fibonacci_Recursive(b, a + b, counter + 1, len);
}
}
}
}
Output:
Enter Length of Fibonacci Series:
10
0 1 1 2 3 5 8 13 21 34
Using Iterative way
In the Iterative way to print the Fibonacci series in C#, we will take the first 2 numbers (0 and 1), print them, and then sum them, print result.
Now, we will use for
loop until we reach the series length entered by the user and keep adding the last 2 numbers and print it.
using System;
namespace Fibonacci
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Length of Fibonacci Series: ");
var len = Convert.ToInt32(Console.ReadLine());
int firstnumber = 0, secondnumber = 1, result = 0;
//print first 2 numbers of fibonacci series
Console.Write("{0} {1}", firstnumber, secondnumber);
//use for loop until length
for (int i = 2; i < len; i++)
{
//get sum of last 2 numbers
result = firstnumber + secondnumber;
//print the number
Console.Write(" {0}", result);
//update variables to get new sum
firstnumber = secondnumber;
secondnumber = result;
}
Console.WriteLine();
}
}
}
Output is as same as above:
Enter Length of Fibonacci Series:
10
0 1 1 2 3 5 8 13 21 34
Calculate nth Fibonacci Series Number
We can easily get nth Fibonacci series number, basically, in this program we will keep looping and adding the last 2 numbers until the nth number is found.
Here is the code using Iterative approach
using System;
namespace Fibonacci
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the Nth number of Fibonacci Series: ");
var len = Convert.ToInt32(Console.ReadLine());
int firstnumber = 0, secondnumber = 1, result = 0;
//use for loop until nth number
for (int i = 2; i < len; i++)
{
//get sum of last 2 numbers
result = firstnumber + secondnumber;
//update variables to get new sum
firstnumber = secondnumber;
secondnumber = result;
}
Console.WriteLine(len + "th Number is: " + result);
Console.WriteLine();
}
}
}
Output:
Enter the Nth number of Fibonacci Series:
5
5th Number is: 3
You may also like to read:
Create a calculator in C# console application
System.Text.Json Serialize / Deserialize Object in C#
Creating Cascading Dropdown list in ASP.NET Core MVC