Print a string of fibonacci recursively in C#

前端 未结 11 644
你的背包
你的背包 2020-12-29 16:05

Can that be done with no while loops?

static void Main(string[] args)
{
    Console.WriteLine(\"Please enter a number\");
    int number = Convert.ToInt32(C         


        
11条回答
  •  生来不讨喜
    2020-12-29 16:20

    Simple and easy solution :

    static void Main(string[] args)
    {
         int number;
    
         Console.WriteLine("enter number");
    
         number = int.Parse(Console.ReadLine());
    
         Console.WriteLine(Recursive(number));
    
         Console.ReadLine();
    }
    
    public static int Recursive(int number)
    {
        if (number <= 2)
        {
            return 1;
        }
        else
        {
            return Recursive(number - 1) + Recursive(number - 2);
        }
    }
    

提交回复
热议问题