Write a program to print the Fibonacci series in c# using Recursion.




Write a program to print the Fibonacci series in c# using Recursion.


class Fibonacci
    {
        // Using Recursion:
        public static int FibonacciSeries(int n)
        {
           //To return the first & second Fibonacci number
            if (n == 0 || n == 1) return n; 
                
            return FibonacciSeries(n - 1) + FibonacciSeries(n - 2);
        }

        public static void Main(string[] args)
        {
            Console.Write("Enter the length of the Fibonacci Series: ");

            int length = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < length; i++)
            {
                Console.Write("{0} ", FibonacciSeries(i));
            }
            Console.ReadLine();
        }
    }


Result:


Comments

Popular posts from this blog

iframe vs embed vs object in HTML 5

Constructor in c#

What is the need of method overriding ???