Table of Contents
Fibonacci Series Logic
Various Method of creating Fibonacci Series
1. Iterative Approach
2. Recursive Method
3. Fibonacci by using Array
How to find the Nth Term of Fibonacci Series?
Method 2
Conclusion
Home Backend Development C#.Net Tutorial Fibonacci Series in C#

Fibonacci Series in C#

Sep 03, 2024 pm 03:34 PM
c# c# tutorial

The Fibonacci Series in C# in the Fibonacci series is one of the famous sequence series. The sequence is 0, 1, 1, 2, 3, 5, 8…. The Fibonacci series starts from zero and one and the next number is the sum of two preceding numbers. It has been said that the Fibonacci Series created by Mr.Leonardo Pisano Bigollo in the 13th century. Fibonacci series is useful for some scenarios. Basically it was originally used to solve the rabbit problem i.e. The number of rabbits born from a pair. There are other problems also in which the Fibonacci sequence is useful.

Fibonacci Series Logic

As in the Fibonacci series, the number is the sum of its two preceding numbers. So if we have a Fibonacci series say 0, 1, 1, 2, 3, 5, 8, 13, 21… According to this next number would be the sum of its preceding two like 13 and 21. So the next number is 13+21=34.

Here is the logic for generating Fibonacci series

F(n)= F(n-1) +F(n-2)

Where F(n) is term number and F(n-1) +F(n-2) is a sum of preceding values.

So if we have series 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89…

According to the logic F(n)= F(n-1) +F(n-2)

F(n)= 55+89

F(n)= 144

The next term would be 144.

Various Method of creating Fibonacci Series

Fibonacci series can be generated in multiple ways.

1. Iterative Approach

This way is the easiest way to generate series.

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespaceFibonacciDemo
{
classProgram
{
staticint Fibonacci(int n)
{
intfirstnumber = 0, secondnumber = 1, result = 0;
if (n == 0) return 0; //It will return the first number of the series
if (n == 1) return 1; // it will return  the second number of the series
for (int i = 2; i<= n; i++)  // main processing starts from here
{
result = firstnumber + secondnumber;
firstnumber = secondnumber;
secondnumber = result;
}
return result;
}
staticvoid Main(string[] args)
{
Console.Write("Length of the Fibonacci Series: ");
int length = Convert.ToInt32(Console.ReadLine());
for(int i = 0; i< length; i++)
{
Console.Write("{0} ", Fibonacci(i));
}
Console.ReadKey();
}
}
}
Copy after login

2. Recursive Method

This is another method to solve this problem.

Method 1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespaceFibonacciDemo
{
classProgram
{
staticint Fibonacci(int n)
{
intfirstnumber = 0, secondnumber = 1, result = 0;
if (n == 0) return 0; //it will return the first number of the series
if (n == 1) return 1; // it will return the second number of the series
return Fibonacci(n-1) + Fibonacci(n-2);
}
staticvoid Main(string[] args)
{
Console.Write("Length of the Fibonacci Series: ");
int length = Convert.ToInt32(Console.ReadLine());
for(int i = 0; i< length; i++)
{
Console.Write("{0} ", Fibonacci(i));
}
Console.ReadKey();
}
}
}
Copy after login

Method 2

using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FibonacciSeries
{
class Program
{
public static void Fibonacci
(
int firstnumber,
int secondnumber,
int count,
int length,
)
{
if (count <= length)
{
Console.Write("{0} ", firstnumber);
Fibonacci(secondnumber, firstnumber + secondnumber, count + 1, length);
}
}
public static void Main(string[] args)
{
Console.Write("Length of the Fibonacci Series: ");
int length = Convert.ToInt32(Console.ReadLine());
Fibonacci(0, 1, 1, length);
Console.ReadKey();
}
}
}
Copy after login

Output:

Fibonacci Series in C#

3. Fibonacci by using Array

Code:

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);
}
}
}
Copy after login

Output:

Fibonacci Series in C#

How to find the Nth Term of Fibonacci Series?

Following are the methods

Method 1

Code:

using System;
namespace FibonacciSeries
{
class Program {
public static int NthTerm(int n)
{
if ((n == 0) || (n == 1))
{
return n;
}
else
{
return (NthTerm(n - 1) + NthTerm(n - 2));
}
}
public static void Main(string[] args)
{
Console.Write("Enter the nth term of the Fibonacci Series: ");
int number = Convert.ToInt32(Console.ReadLine());
number = number - 1;
Console.Write(NthTerm(number));
Console.ReadKey();
}
}
}
Copy after login

The above code is to find the nth term in the Fibonacci series. For example, if we want to find the 12th term in the series then the result would be 89.

Method 2

(O(Log t) Time).

There is one another recurrence formula that can be used to find t’th Fibonacci Number If t is even then = t/2:

F(t) = [2*F(k-1) + F(k)]*F(k)

If t is odd then k = (t + 1)/2

F(t) = F(k)*F(k) + F(k-1)*F(k-1)

Fibonacci matrix

After getting determinant, we will  get (-1)t = Ft+1Ft-1 – Ft2

FmFt + Fm-1Ft-1 = Fm+t-1

By putting t = t+1,

FmFt+1 + Fm-1Ft = Fm+t

Putting m = t

F2t-1 = Ft2 + Ft-12

F2t = (Ft-1 + Ft+1)Ft = (2Ft-1 + Ft)Ft

To get the formula we will do the following

If t is even, put k = t/2

If t is odd, put k = (t+1)/2

So by sorting these numbers we can prevent the constantly using memory space of STACK. It gives time complexity of O(n). The recursive algorithm is less efficient.

Code:

int f(n) :
if( n==0 || n==1 )
return n;
else
return f(n-1) + f(n-2)
Copy after login

Now when the above algorithm run for n=4

fn(4)

f(3)             f(2)

f(2)   f(1)     f(1)   f(0)

f(1)  f(0)

So it’s a tree. For calculating f(4) we need to calculate f(3) and f(2) and so on.For a small value of 4, f(2) is calculated twice and f(1) is calculated thrice. This number of additions will grows for large numbers.

There is a conjecture that the number of additions required for calculating f (n) is f (n+1) -1.

Conclusion

Here the iteration method is always preferred because it has a faster approach to solve this kind of problem. 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.

The above is the detailed content of Fibonacci Series in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1264
29
C# Tutorial
1237
24
Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

How to change the format of xml How to change the format of xml Apr 03, 2025 am 08:42 AM

There are several ways to modify XML formats: manually editing with a text editor such as Notepad; automatically formatting with online or desktop XML formatting tools such as XMLbeautifier; define conversion rules using XML conversion tools such as XSLT; or parse and operate using programming languages ​​such as Python. Be careful when modifying and back up the original files.

See all articles