• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
EDUCBA

EDUCBA

MENUMENU
  • Resources
        • Java Tutorials

          • Cheat Sheet Java
          • Cheat Sheet Python
          • C# vs Js
        • Java Tutorials
        • Python Tutorials

          • Angular 5 vs Angular 4
          • Careers in Python
          • Kali Linux vs Ubuntu
        • Python Tutorials
        • Top Differences

          • Cheat Sheet JavaScript
          • Python Interview Questions
          • Cloud Computing or Virtualization
        • Top Differences
        • Others

          • Resources (A-Z)
          • Top Interview Question
          • Programming Languages
          • Web Development Tools
          • HTML CSS Tutorial
          • Technology Basics
          • Technology Careers
          • View All
  • Free Courses
  • All Courses
        • Certification Courses

          Software Development Course 2
        • All in One Bundle

          All-in-One-Software-Development-Bundle
        • Become a Python Developer

          Python-Certification-Training
        • Others

          • Java Course
          • Become a Selenium Automation Tester
          • Become an IoT Developer
          • Ruby on Rails Course
          • Angular JS Certification Training
          • View All
  • 600+ Courses All in One Bundle
  • Login

Fibonacci Series in C#

Home » Software Development » Blog » Programming Languages » Fibonacci Series in C#

fibonacci series in c#

Introduction to Fibonacci Series in C#

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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();
}
}
}

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();
}
}
}

Popular Course in this category
Cyber Week Sale
Python Certification Training (36 Courses, 12+ Projects) 36 Online Courses | 12 Hands-on Projects | 187+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.8 (3,488 ratings)
Course Price

View Course

Related Courses
Java Training (38 Courses, 26 Projects)Programming Languages Training (41 Courses, 13+ Projects)

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();
}
}
}

Output:

output1 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);
}
}
}

Output:

output2 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();
}
}
}

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)

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.

Recommended Articles

This is a guide to Fibonacci Series in C#. Here we discuss Fibonacci Series logic with Different methods and how to find the nth term of the Fibonacci Series. You can also go through our other related articles to learn more-

  1. Fibonacci Series in C
  2. C# Compilers
  3. C# Commands
  4. C# For Loop
  5. Guide to Fibonacci Series in C++

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Reader Interactions
Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar
Technology Blog Tutorials
  • Programming Languages
    • Recursive Function in C
    • VB.Net Loops
    • Overloading and Overriding in C#
    • Break in C#
    • Loops in VBScript
    • Yield Keyword in C#
    • Multidimensional Array in PHP
    • Random Number Generator in PHP
    • PHP File Handling
    • Features of C++
    • C# Generics
    • VB.Net Events
    • Switch Statement in C#
    • Multi-Dimensional Arrays in C++
    • Iterators in C#
    • C# Keywords
    • Continue Statement in C++
    • Random Number Generator in C#
    • Virtual Keyword in C#
    • Method Overloading in C#
    • PHP Change Date Format
    • Palindrome in C++
    • Static Method in PHP
    • C++ Data Types
    • Sorting in C#
    • Get IP Address in PHP
    • C Programming Matrix Multiplication
    • PHP Form
    • Sealed Class in C#
    • C++ Keywords
    • Goto Statement in C
    • Static Keyword in C
    • Square Root in PHP
    • Inheritance in PHP
    • Associative Array in PHP
    • While Loop in PHP
    • Object in C++
    • Continue Statement in C
    • PHP Magic Constants
    • Abstract Class in C++
    • PHP Constants
    • PHP Recursive Function
    • Multidimensional Array in C
    • Break Statement in C
    • PHP Booleans
    • What is Type Casting?
    • Swapping in PHP
    • PHP Keywords
    • For Loop in PHP
    • 3D Arrays in C++
    • Patterns in C#
    • Access Modifiers in PHP
    • Arrays in PHP
    • For Loop in C
    • C Keywords
    • Objects in C#
    • Sorting in PHP
    • PHP Integer
    • Access Modifiers in C#
    • PHP Float
    • String Array in C++
    • Break Statement in C++
    • Swapping in C++
    • Palindrome in C#
    • Virtual Keyword in C++
    • Recursive Function in C++
    • Overloading and Overriding in C++
    • String Array in C#
    • Star Patterns in PHP
    • Hashing Function in C
    • Destructor in C++
    • Pointers in C#
    • Math Functions in C
    • Variables in C++
    • Overriding in C#
    • Recursive Function in C#
    • Namespaces in C#
    • 2D Arrays in C#
    • Heap Sort in C++
    • Math Functions in C#
    • Factorial Program in C++
    • Math Functions in C++
    • C++ Garbage Collection
    • Sorting in C++
    • Access Modifiers in C++
    • Constructor and Destructor in C++
    • Best C++ Compiler
    • Heap Sort in C
    • Arrays in C#
    • Constructor in C
    • Exception Handling in C#
    • C# Jagged Arrays
    • If Statement in C
    • Destructor in C#
    • Palindrome Program in C++
    • Reverse Number in C++
    • VB.Net for Loop
    • Abstract Class in PHP
    • Fibonacci Series in C#
    • Fibonacci Series in C++
    • Reverse Number in C
    • Palindrome in PHP
    • Encapsulation in C
    • What is LINQ?
    • Swapping in C#
    • Best Programming Languages
    • C Storage Classes
    • Array Functions in C
    • Square Root in C++
    • Overriding in C++
    • PHP Database Connection
    • Pointers in C++
    • Arrays in C Programming
    • Overloading in C++
    • Sessions in PHP
    • Encapsulation in PHP
    • Star Patterns in C
    • Overloading in PHP
    • Cookie in PHP
    • Number Patterns in C
    • C++ Array Functions
    • Copy Constructor in C++
    • Pointers in C
    • Fibonacci Series in C
    • Square Root in C
    • Palindrome in C Program
    • PHP While Loop
    • Method Overriding in C#
    • Factorial in PHP
    • Random Number Generator in C
    • Patterns in PHP
    • Data Binding in ASP.NET
    • Variables in C
    • C# Compilers
    • Object in PHP
    • Sorting in C
    • PHP Switch Statement
    • PHP Do While Loop
    • Patterns in C++
    • Overloading in C#
    • 2-D Arrays in C
    • Polymorphism in C#
    • While Loop in C
    • PHP Compiler
    • Socket Programming in PHP
    • Encapsulation in C++
    • Variables in C#
    • Star Patterns In c++
    • UNIX Administrator
    • What is .NET Core
    • What is ADO.NET?
    • Switch Statement in C++
    • Copy Constructor in C#
    • Caching In ASP.NET
    • C# For Loop
    • Arrays in C++
    • Factorial in C
    • Patterns in C Programming
    • C# While Loop
    • ASP.NET Server Controls
    • What is StringBuilder in C#
    • Variables in PHP
    • Overriding in PHP
    • Constructor in PHP
    • 3D Arrays in C
    • Data Types in C
    • R vs Python
    • C# do-while loop
    • ASP.NET Framework
    • C# if Statement
    • PHP Math Functions
    • What is Raspberry Pi?
    • Kotlin Operators
    • Kotlin vs Scala
    • Inheritance in C#
    • R Programming Language
    • R-studio-Functions-Software
    • What Is Django
    • Go Operators
    • Scala Operators
    • Introduction To C++
    • C Operators
    • C++ Operators
    • Install R
    • R Operators
    • How to Connect Database to PHP
    • R String Functions
    • What is Design Pattern in C#
    • C++ Programming Language
    • Haskell Programming Language
    • Algorithms and Cryptography
    • Ruby Programming Practice
    • Programming Languages for Algorithms
    • Programming for beginners
    • Scheme Programming Language
    • Scratch Programming examples
    • Scala Functional Approach
    • Drupal Web Development
    • Web Development Apps in Go Programming
    • Fundamentals of Computer Programming
    • What is COBOL
    • Cheat sheet for UNIX
    • Applications of C++ In Real World
    • Applications Of Blockchain
    • Uses of .Net
    • Uses of Unix
    • Uses Of Ruby
    • Ruby On Rails Language
    • What Is NOSQL
    • Data Structures and Algorithms C++
    • Uses of Scala
    • Advantages of OOP
    • Introduction to C
    • Swift Operators
    • Advantages of Rails
    • What is PHP Array
    • What is PHP
    • What is C++
    • What is Perl
    • Perl Operators
    • What is C
    • What is Multithreading in C#
    • Python vs JavaScript
    • What is Kotlin
    • PHP Frameworks
    • What is TypeScript
    • What is Groovy
    • What is a Programming Language
    • What is Drupal
    • What is Inheritance in Programming
    • C++ Alternatives
    • C# Functions
  • Database Management (71+)
  • Ethical Hacking Tutorial (33+)
  • HTML CSS Tutorial (47+)
  • Installation of Software (54+)
  • Top Interview question (188+)
  • Java Tutorials (196+)
  • JavaScript (71+)
  • Linux tutorial (32+)
  • Network Security (85+)
  • Python Tutorials (89+)
  • Software Development Basics (321+)
  • Software Development Careers (38+)
  • SQL Tutorial (33+)
  • String Functions (12+)
  • Technology Commands (38+)
  • Top Differences (368+)
  • Web Development Tools (33+)
  • Mobile App (60+)
Technology Blog Courses
  • Python Training Certification
  • Online Java Course
  • Programming Languages Training
Footer
About Us
  • Who is EDUCBA?
  • Sign Up
  •  
Free Courses
  • Free Course Programming
  • Free course Python
  • Free Course Java
  • Free Course Javascript
  • Free Course on SQL
  • Free Course on Web Design
  • Free HTML Course
  • Free Android App Development Course
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • Ruby on Rails Course
  • ASP.NET Course
  • VB.NET Course
  • Bootstrap Training Course
  • Become a Linux System Administrator
  • PHP Course
  • Joomla Training
  • HTML Course
Resources
  • Resources (A To Z)
  • Java Tutorials
  • Python Tutorials
  • Top Differences
  • Top Interview Question
  • Programming Languages
  • Web Development Tools
  • HTML CSS Tutorial
  • Technology Basics
  • Technology Careers
  • Ethical Hacking Tutorial
  • SQL Tutorials
  • Digital Marketing
Apps
  • iPhone & iPad
  • Android
Support
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions

© 2019 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Let’s Get Started
Please provide your Email ID
Email ID is incorrect

Cyber Week Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) View More

Cyber Week Offer - Cyber Week Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) View More