Write M file with while loop, which computes factorial of any x, (x=12).
24 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
N/A
am 21 Okt. 2020
Beantwortet: Chenguang Yan
am 21 Okt. 2020
Directions:
Define x=12, Start with some value of factorial say Fact=1; - while loop condition will be x > 1 - compute factorial by multiplying the number with Fact - Reduce number by 1 - Close the while loop - Display the Fact
This is what I have so far (it is incorrect):
x=12;
while x>1
fact = x*(x-1);
x=x-1
end
disp(fact);
0 Kommentare
Akzeptierte Antwort
Ameer Hamza
am 21 Okt. 2020
Bearbeitet: Ameer Hamza
am 21 Okt. 2020
You need to update the value of factorial at each step by using its value from the previous step
x=12;
fact = 1;
while x>1
fact = fact*x;
x=x-1;
end
disp(fact);
Weitere Antworten (1)
Chenguang Yan
am 21 Okt. 2020
f = 12;
x = f;
fact = x;
while x>1
x = x-1;
fact = fact*x;
end
disp(fact)
%
assert(isequal(fact,factorial(f)))
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!