在不使用任何循环的情况下计算数字的阶乘. [英] Calculate the factorial of a number without using any loop.
本文介绍了在不使用任何循环的情况下计算数字的阶乘.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在一次采访中,有人要求我在不使用任何循环的情况下计算数字的阶乘,甚至我尝试了但我无法完成.请帮助我如何在不使用任何循环的情况下在c#中计算阶乘.就在发布此问题之前,我在google上尝试过,但是所有答案都使用了循环.
因此,我再次将查询发布在最佳的教程资源上.
请帮助我.
谢谢.
In an interview someone asked me to calculate the factorial of a number without using any loop, nd even I tried but I was not able to complete it. Kindly help me how we can calculate factorial in c# without using any loop. Just Before posting this question I tried on google , but all the answers used loop.
So Once again I posted my query on my best tutorial resource.
Kindly help me.
Thanks.
推荐答案
我不确定这是否是正确的答案,应该使用递归函数来实现.
I am not sure this is the correct answer it should be possible using recursive fuction.
int fact(int n)
{
if(n==0)
return 1;
else
return n * fact(n-1);
}
Hi,
public int fact(int n)
{
if (n==0) return 1;
else return n*fact(n-1)
}
谢谢,
Bh @ gyesh
Thanks,
Bh@gyesh
您可以使用递归方法,该方法会多次调用自身直到满意为止.
检查以下链接以获取更多信息
使用C#的递归方法 [ C#-使用递归函数和不使用递归函数 [
you can use recursive method which calls itself so many times until being satisfied.
check below links for more information
Recursive methods using C#[^]
C# - Factorial With and With out using Recursive Functions[^]
这篇关于在不使用任何循环的情况下计算数字的阶乘.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文