Does your company or startup need an expert? If so, write to me to talk about possibilities of professional cooperation.
PROGRAMMING on-line course is approaching! Write to me if you are interested in free early access.

MARCIN.com

Marcin Jamro, PhD, DSc

How to calculate number from Fibonacci series?

C#  |  .NET  |  Algorithms

The Fibonacci number can be calculated according to the formula:

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

As an example:

F(5) = F(3) + F(4)
F(5) = F(1) + F(2) + F(2) + F(3)
F(5) = F(1) + F(0) + F(1) + F(0) + F(1) + F(1) + F(2)
F(5) = F(1) + F(0) + F(1) + F(0) + F(1) + F(1) + F(0) + F(1)
F(5) = 1 + 0 + 1 + 0 + 1 + 1 + 0 + 1 = 5

The code calculating the Fibonacci number is presented below:

long Fibonacci(int n)
{
    if (n == 0) { return 0; }
    if (n == 1) { return 1; }

    long a = 0;
    long b = 1;
    for (int i = 2; i <= n; i++)
    {
        long result = a + b;
        a = b;
        b = result;
    }

    return b;
}

You can check this code by the following lines:

Console.WriteLine($"F(0) = {Fibonacci(0)}");
Console.WriteLine($"F(1) = {Fibonacci(1)}");
Console.WriteLine($"F(5) = {Fibonacci(5)}");
Console.WriteLine($"F(10) = {Fibonacci(10)}");

The results are shown as follows:

F(0) = 0
F(1) = 1
F(5) = 5
F(10) = 55

This entry can contain content, including parts of code, from one of my books, namely from the Second Edition of C# Data Structures and Algorithms, published by Packt Publishing in 2024. If you want to get a bit more detailed description of calculating a number from the Fibonacci series, as well as about many other algorithms, I encourage you to take a look at the mentioned book. You can buy it here.

The content, including any part of code, is presented without warranty, either express or implied. The author cannot be held liable for any damages caused or alleged to have been caused directly or indirectly by any content shown on this website.

Hello, I am Marcin

Reliable entrepreneur with 10+ years of companies operation, such as CEO at a few IT companies. I was an author of a few software & hardware products, still open to new ideas & cooperation.

Helpful expert with 10+ years of experience, together with PhD and DSc in Computer Science. I was an author of books and publications, as well as an expert in international projects.

Experienced developer with 10+ years of development and 100+ completed projects. I worked on various complex international projects, e.g. at Microsoft in USA and as CTO at a few companies. I have MCP, MCTS and MCPD certificates.

You can read more about me in my short bio. I am waiting for contact at [email protected], as well as at my Facebook and LinkedIn profiles.

Books

I am an author of a few books and numerous publications, also in high-quality international scientific journals.

If you want to learn data structures and algorithms in the context of C#, let's take a look at my newest book. It is the second edition of C# Data Structures and Algorithms. You can buy it here.

Projects

Do you like traveling? If so, discover an amazing world with local guides, right here and right now at: https://camaica.com

Travels

I love travels, learning new cultures and regions, meeting outstanding people, as well as taking pictures of beautiful places. Take a look at my travel diary.