You are on page 1of 3

1/15/2015 C# Fibonacci Sequence

Go

C# Fibonacci Sequence C# :  .NET :  Method

Fibonacci numbers are a fascinating
sequence. This sequence models and predicts
I Am A Doctor Living In A
financial markets and natural phenomena. 2bhk Rented House,
Bachelor, Looking For A
Pimpri, Pune
We can compute Fibonacci numbers with Tenant, Preferably Bachelor 2
6,000
Share The Flat
Rs.
recursion. This can be slow. It is also possible < 500,5... Sq­Ft Reply to Ad

to use iteration. 2 Bedrroms , 950 Sq Ft,
Ravet, Pune

Example
Pimpri Chinchwad, Pune

Rs. 7,000
First, this is an iterative method. Reply to Ad

Conceptually, an iterative Fibonacci
method stores the result of the previous
Fibonacci number before computing the
next one. This reduces the time required.

Note:
To compute a Fibonacci number at a
certain position N, we have to loop through all previous numbers
starting at position 0.
For

Based on:
► How to Program in C#
.NET 4.5 ► C# Method
► Fibonacci Numbers
C# program that computes Fibonacci iteratively

using System;

class Program
{
    public static int Fibonacci(int n)
    {
  int a = 0;
  int b = 1;
  // In N steps compute Fibonacci sequence iteratively.
  for (int i = 0; i < n; i++)
  {
      int temp = a;
      a = b;
      b = temp + b;
  }
  return a;
    }

    static void Main()
    {
  for (int i = 0; i < 15; i++)
  {
      Console.WriteLine(Fibonacci(i));
  }
    }
}

Output

0
1
1
2
3
5

http://www.dotnetperls.com/fibonacci 1/3
1/15/2015 C# Fibonacci Sequence
8
13
21
34
55
89
144
233
377

If you just want to list Fibonacci numbers,
you could change Fibonacci() to simply use
Console.WriteLine(a). This would make the
method print out each number as it is
computed, which would reduce the total
amount of computation required.

But:
The time spent printing to the Console
would dominate the program's real­world
runtime.
Console.WriteLine

Overflow
One problem with this implementation is that the
int types will overflow past the 47th Fibonacci
number. To solve this problem, you can change
Fibonacci() to return double. Also change a, b, and
temp to be of type double.
Int
Double

Discussion
The Fibonacci sequence begins with
zero. Fibonacci himself, in 1202, began
it with 1, but modern scientists just use
his name, not his version of the
sequence. I tested the output of the
program and it is correct.

Fibonacci began the sequence not
with 0, 1, 1, 2, as modern
mathematicians do but with 1, 1, 2.
Fibonacci: Wikipedia

Summary
Fibonacci numbers don't just reveal some
interesting aspects of the world. They also
teach us about computer programming. By
storing the previous value in a loop iteration,
we avoid a tremendous amount of
computational work.

And:
In the rare cases where Fibonacci numbers are critical, an iterative
solution dramatically improves software.

http://www.dotnetperls.com/fibonacci 2/3
1/15/2015 C# Fibonacci Sequence

► C# Example
► Learn C# Programming
► How to Program in C#

Flat On Rent At Pimple Gurav For Rent Unfurnished 1BHK

Pimple Gurav, pune 500 ­ 1000 Sqft. | Pimple Gurav, Pune 500 Sqft. |


Unfurnished | Unfurnished |
525 Sq­Ft Rs.7,500 Reply to Ad
Individual
500 Sq­Ft Rs.7,000 Reply to Ad
Individual

http://www.dotnetperls.com/fibonacci 3/3

You might also like