Find all the Fibonacci numbers for a given limit

Fibonacci numbers are a series of numbers in which each number is formed by adding previous two numbers in the series. For example, we start by 1 and 2, and then repetitively add the add the numbers to result in the next number.

For a given max limit 10, we would want to find the first 10 Fibonacci numbers.

Series: 1,2,3,5,8,13,21,34,55,89


        public static List<int> DoGetFibonacci(int n)
        {
            int a = 1, b = 2, c = 0;
            List<int> series = new List<int>();

            series.Add(a);
            series.Add(b);

            while (series.Count < n)
            {
                c = a + b;
                a = b;
                b = c;
                
                series.Add(c);
            }

            return series;
        }
        

How it works:

The core of Fibonacci series is adding up previous two numbers a and b to form the next number c. Taking this as the base we build the logic and put it in a loop to iteratively generate the series.

Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Privacy Overview
Referbruv

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

3rd Party Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.