阶乘实现(C#递归)

开门见山

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            int n;
            int.TryParse(Console.ReadLine(), out n);
            Console.WriteLine(Factorial(n));
            Console.ReadKey();
        }

        static int Factorial(int n)
        {
    
    
            if (n == 0)
            {
    
    
                return 1;
            }
            return n * Factorial(n - 1);
        }
    }
}

运行:
在这里插入图片描述
注意
最高支持到31阶乘:
在这里插入图片描述
34阶乘直接溢出为0

猜你喜欢

转载自blog.csdn.net/weixin_44737486/article/details/109459746