MATLAB code find factorial of n num
Info
This question is locked. Reopen it to edit or answer.
Show older comments
can you please Write a MATLAB code to Find factorial of a given number N?
3 Comments
KSSV
on 18 Jun 2020
You search in google/ mathworks you will get plenty...
What have you tried?
Murathan Cuya
on 18 Jun 2020
Steven Lord
on 20 Sep 2024
Accepted Answer
More Answers (4)
ANSHU PRIYA
on 12 Jun 2021
okh so we are intresting in to finding the value of 7 factorial
programming:-
%calculation of factorial
n=7;
factvalue=1;
for i=1:7
factvalue=factvalue*i
end
3 Comments
Walter Roberson
on 13 Jun 2021
Why bother assigning to n if you are not going to use it?
ANSHU PRIYA
on 13 Jun 2021
its obvious !because the notation of factorial are denoted by n!
here we are finding the value of n! where n=7.
thats why we assinginf here n=7
Walter Roberson
on 13 Jun 2021
So if I coded
%calculation of factorial
n=7;
factvalue=1;
for i=1:9
factvalue=factvalue*i
end
then afterwards factvalue would hold n! = 7! ? Or would it hold 9! ? Where is the connection between the value of n and the value calculated?
Ahmet Zahit Akpinar
on 10 Mar 2022
Moved: DGM
on 21 Sep 2024
I made it for 10! but you can change 10 by any value
n=1; i=1;
while i<=10
n=n*i;
i=i+1;
fprintf('n=%d\n')
end
1 Comment
What is the point of printing a bunch of instances of "n=" repeatedly to console? Why is anything printed in the loop? Why use a while loop instead of a for loop?
What's wrong with
n = 10; % isolate the parameter
result = 1;
for k = 1:n
result = result*k;
end
result
... or just
result = prod(1:n)
... or, you know
result = factorial(n)
Darya
on 15 Nov 2022
Edited: Walter Roberson
on 15 Nov 2022
n=input('n=')
if n>=0&&n==fix(n)
f=1;
for i=2:n
f=f*1;
end
else
disp('ererr')
end
f
1 Comment
Walter Roberson
on 15 Nov 2022
f=f*1;
No, it should be
f=f*i;
Hugo
on 18 Oct 2023
0 votes
hecho de función = factorial(n)
hecho = 1; % caso base, para F(0);
for i = 1:n %recorriendo valores de 1:n para multiplicar cada valor;
hecho = hecho*i; %multiplicando con nuestros valores anteriores.
fin
fin hecho = 1; % caso base, para F(0); for i = 1:n %recorriendo valores de 1:n para multiplicar cada valor; hecho = hecho*i; %multiplicando con nuestros valores anteriores. finfin
1 Comment
Walter Roberson
on 18 Oct 2023
"fin" is not used in MATLAB
This question is locked.
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!