For and while loops

6 vues (au cours des 30 derniers jours)
Danny Maefengea
Danny Maefengea le 22 Juin 2020
Commenté : Walter Roberson le 22 Juin 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 commentaire
KSSV
KSSV le 22 Juin 2020
What have you tried?

Connectez-vous pour commenter.

Réponse acceptée

Ashish Azad
Ashish Azad le 22 Juin 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 commentaire
Walter Roberson
Walter Roberson le 22 Juin 2020
We recommend against providing complete solutions to homework problems.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by