Azzera filtri
Azzera filtri

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

1 visualizzazione (ultimi 30 giorni)
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

Risposte (1)

Image Analyst
Image Analyst il 27 Dic 2016
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 Commenti
Rola Zayn
Rola Zayn il 27 Dic 2016
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 il 27 Dic 2016
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.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by