Print a string of fibonacci recursively in C#

前端 未结 11 715
你的背包
你的背包 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:41

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static int Main(string[] args)
            {
    
                int n, i = 0, c;
                Console.WriteLine("Enter the number of terms:");
                n = Convert.ToInt16(Console.ReadLine());
    
                Console.WriteLine("Fibonacci series\n");
    
                for (c = 1; c <= n; c++)
                {
                    int result = FibonacciFunction(i);
                    Console.Write(result + " " );
                    i++;
                }
                Console.WriteLine();
                return 0;
            }
    
            public static int FibonacciFunction(int n)
            {
                if (n == 0)
                {
                    return 0;
                }
                else if (n == 1)
                {
                    return 1;
                }
                else
                {
                    return (FibonacciFunction(n - 1) + FibonacciFunction(n - 2));
                }
            }
    
        }
    }
    

提交回复
热议问题