Effacer les filtres
Effacer les filtres

While Loop Homework Question

12 vues (au cours des 30 derniers jours)
Nicholas Bieganski
Nicholas Bieganski le 4 Nov 2020
Hey everyone, I have a homework assignment due today and I can't figure out this question: Write a MATLAB repetition structure to determine how many terms in the sum 1+2+3+... are required for the sum to exceed one million (1e+6). Display how many terms are required and what is the first sum that exceeds one million in one sentence in the command window. Could someone help me out? I really can't find a way to figure it out so it would be awesome and really helpful is someone could help me.
  2 commentaires
Stephen23
Stephen23 le 4 Nov 2020
@Nicholas Bieganski: please show us what you have tried so far.
Nicholas Bieganski
Nicholas Bieganski le 4 Nov 2020
@Stephen Cobeldick this is what I have so far.
k=1;
while (a<1000000)
k=k+1;
a(k)=1+k+(k+1);
end
These loops are super confusing for me, so I have no idea if I am even close

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 4 Nov 2020
You were close, here is your code with a few small changes:
k = 0;
a = k;
while a<1e6
k = k+1;
a = k+a;
end
disp(k)
1414
disp(a)
1000405
  1 commentaire
Nicholas Bieganski
Nicholas Bieganski le 4 Nov 2020
Ok, thank you so much! I thought I was going in the right direction, but the answer was saying otherwise so I was lost. But thanks again I really appreciate it!

Connectez-vous pour commenter.

Plus de réponses (1)

João Mendes
João Mendes le 4 Nov 2020
Can you use for loops? or only while?
  1 commentaire
Nicholas Bieganski
Nicholas Bieganski le 4 Nov 2020
For this question we were only allowed to use while loops.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by