Using fprintf to repeat the statement multiple times

29 vues (au cours des 30 derniers jours)
Liz
Liz le 30 Nov 2020
Commenté : Liz le 30 Nov 2020
The question is:
Prompt the user for an integer n and print "I love this stuff!" n times.
I thought I went around it by doing this: however I am not sure where to put the n number in and how to do it.
>> for iv = 1:n
inputnum = input('Enter a integer: ');
fprintf('I love this stuff')
end

Réponse acceptée

Jan
Jan le 30 Nov 2020
Modifié(e) : Jan le 30 Nov 2020
Almost working.
n = input('Enter a integer: ');
for k = 1:n
fprintf('I love this stuff\n')
end
Ask for n before the loop, not inside.
  1 commentaire
Liz
Liz le 30 Nov 2020
Oh that makes so much sense! Thanks so much!

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

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by