필터 지우기
필터 지우기

Info

This question is locked. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

MATLAB code find factorial of n num

조회 수: 299 (최근 30일)
Murathan Cuya
Murathan Cuya 2020년 6월 18일
Locked: DGM 2024년 9월 21일 12:29
can you please Write a MATLAB code to Find factorial of a given number N?
  댓글 수: 3
Murathan Cuya
Murathan Cuya 2020년 6월 18일
ı need n as a input. ı searvched and saw some explains but ı didnt like them. ı really am new about this. ı dont want factorial(n). ı want a independed place from the function to put inputs.
Steven Lord
Steven Lord 2024년 9월 20일 23:20
BTW the factorial function is already part of MATLAB, see factorial.

채택된 답변

Nipun Agarwal
Nipun Agarwal 2020년 6월 18일
Hey,
The factorial of a number is calculated as
F(n) = (n-1)*(n-2)*(n-3)…….1 and F(0) = 1 always;
So we start from the right most side like F(p) where p = 1 initially and keep incrementing the value of p till n; The resultant value is your answer. I have written the function with variable n. replace n with any integer to get its factorial number;
Attaching the code for your reference.
function fact = factorial(n)
fact = 1; %base case, for F(0);
for i = 1:n %looping through values from 1 : n to multiply each value;
fact = fact*i; %multiplying with our previous values.
end
end
The fact will have your desired factorial of the integer you called function with.
  댓글 수: 1
Walter Roberson
Walter Roberson 2021년 6월 13일
Factorial starts with n* not (n-1)* . The code has it right.

추가 답변 (4개)

ANSHU PRIYA
ANSHU PRIYA 2021년 6월 12일
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
ANSHU PRIYA
ANSHU PRIYA 2021년 6월 13일
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
Walter Roberson 2021년 6월 13일
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
Ahmet Zahit Akpinar 2022년 3월 10일
이동: DGM 2024년 9월 21일 12:18
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
DGM
DGM 2024년 9월 21일 12:27
편집: DGM 2024년 9월 21일 12:28
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
result = 3628800
... or just
result = prod(1:n)
result = 3628800
... or, you know
result = factorial(n)
result = 3628800

Darya
Darya 2022년 11월 15일
편집: Walter Roberson 2022년 11월 15일
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
Walter Roberson
Walter Roberson 2022년 11월 15일
f=f*1;
No, it should be
f=f*i;

Hugo
Hugo 2023년 10월 18일
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
Walter Roberson
Walter Roberson 2023년 10월 18일
"fin" is not used in MATLAB

This question is locked.

카테고리

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