fibonacci series in c# using recursion

Fibonacci Series using Recursion and Loop in ASP.Net C#

Hello everyone, I am going to share the code sample for the recursion fibonacci series using c# with console application as given below.


public class FibonacciSeries
{
    static void Main(string[] ax)
    {
        Console.WriteLine("Enter the number.");
        int number = Convert.ToInt32(Console.ReadLine());

        //USING LOOP FOR GET FIBONACCI SERIES.
        FibonicciAlgo(number);         

        //USING RECURCIVE METHOD FOR GET FIBONACCI SERIES.
        Fibonacci_Recurcive(0, 1, 1, number);
    }

    /// <summary>
    /// USING LOOP FOR GET FIBONACCI SERIES.
    /// </summary>
    private static void FibonicciAlgo(int number)
    {
        int a = -1;
        int b = 1;
        for (int i = 0; i < number; i++)
        {
            int c = b + a;
            a = b;
            b = c;
            Console.Write("{0} ", b);
        }
        Console.ReadLine();
    }

    /// <summary>
    /// USING RECURCIVE METHOD FOR GET FIBONACCI SERIES.
    /// </summary>
    private static void Fibonacci_Recurcive(int a, int b, int counter, int number)
    {
        if (counter <= number)
        {
            Console.Write("{0} ", a);
            Fibonacci_Recurcive(b, a + b, counter + 1, number);
        }
        Console.ReadLine();
    }

}

The live output


ANIL SINGH

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Buy Books Online at Best Prices

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.