Hi..i want to write an m-file that takes an integer, computes the factorial and returns the answer..is this correct?

1 次查看(过去 30 天)
Function f=fact(num) If num<0 f='no factor'; else if num==0 f=1; Else f=1; While num>=1 f=f*num; num=num-1; End End End

回答(1 个)

Image Analyst
Image Analyst 2016-12-27
No, there are numerous syntax errors there, as the lint in the code editor will point out to you with red lines or red error messages.
For some of them: function, if, while, and else do not have a capital letter at the start of them. And "Else if" is one lower case word "elseif".
The indenting is also messed up. In the MATLAB code editor you can type control a (to select All text) and then control i (to properly indent all the code). To put it here, paste the code here, highlight it, and then (not before) click the {} Code button:
  2 个评论
Rola Zayn
Rola Zayn 2016-12-27
Yes i know the code seems messed up here ..i correctly put it in the body but i dont know why they appeared crammed up like this
Image Analyst
Image Analyst 2016-12-27
That's why I told you how to fix it, and the last link I gave basically tells you the same thing, but also has other good info. Give it a try - it's a good thing to learn.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by