Recursive factorial algorithm not working
10 次查看(过去 30 天)
显示 更早的评论
So I have to create a function that takes an input value and then generates it factorial using a recursive algorithm.
This is what I have so far, but I just can't seem to get it to work. any help would be great thanks
function y = FactRecur(n)
for n=input('give integer ')
y = n ;
if n == 0
y = 1 ;
else
y = y * FactRecur(n-1);
end
disp(y)
end
0 个评论
采纳的回答
Image Analyst
2017-11-26
Don't ask for n.
Try this:
function y = FactRecur(n)
y = n ;
if n == 0
y = 1 ;
else
y = y * FactRecur(n-1);
end
disp(y)
Now call it, for example to find 5! :
result = FactRecur(5)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Encryption / Cryptography 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!