For loop within a function?
Ältere Kommentare anzeigen
How can I put a for loop inside of a function? I'm trying to get the factorial of an input. Any given input.
Like if I pull up the function an put any number inside, to have its factorial be the output.
Akzeptierte Antwort
Weitere Antworten (1)
Stalin Samuel
am 26 Mär. 2016
I don't understand why do you going for for loop instead of using the inbuilt factorial function
n = 3;%input value
f = factorial(n)
2 Kommentare
Juan Marquez
am 26 Mär. 2016
Stalin Samuel
am 26 Mär. 2016
function f = fact(n)
f = 1;
for itr = 2:n
f = f*itr;
end
end
save the above code as separate file in your working directory and from main file use the below code to call the function
n = 3;%input value
f = fact(n)
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!