For and while loops

2 Ansichten (letzte 30 Tage)
Danny Maefengea
Danny Maefengea am 22 Jun. 2020
Kommentiert: Walter Roberson am 22 Jun. 2020
Hi everyone,
How can I write a Matlab function that takes an input integer n and computes the following
n!=n×(n1)×(n2)×2×1 by using:
The while loop and name it getFacWhile(n).
The for loop and name it getFacFor(n).
  1 Kommentar
KSSV
KSSV am 22 Jun. 2020
What have you tried?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Ashish Azad
Ashish Azad am 22 Jun. 2020
function F = getFacWhile(n)
F=1;
while n>1
F=F*n;
n=n-1;
end
end
function F = getFacWhile(n)
F=1;
for i=1:n
F=F*i;
end
end
  1 Kommentar
Walter Roberson
Walter Roberson am 22 Jun. 2020
We recommend against providing complete solutions to homework problems.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by