Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

New to matlab, need help with problem..

1 vue (au cours des 30 derniers jours)
Mohammed Safwat
Mohammed Safwat le 11 Déc 2017
Clôturé : MATLAB Answer Bot le 20 Août 2021
I am suppose to create a loop to calculate the sum 1+2+3+...+300. Display the total after each 50 (i.e. 50, 100, 150 …) I know I need an if statement in my for loop but have no idea what to do. This is what I have thus far..
Total = 0;
for z = 1:299
Total = Total + z*(z+1)
if z(50 == 0)

Réponses (2)

James Tursa
James Tursa le 11 Déc 2017
Modifié(e) : James Tursa le 11 Déc 2017
Good start. Thank you for posting your code. Some help:
- Have the top value of your loop be 300, not 299
- Have the Total simply add in z, not z*(z+1)
- Use the mod function in your if-test. E.g.,
if mod(z,50)==0

Mohammed Safwat
Mohammed Safwat le 11 Déc 2017
Modifié(e) : James Tursa le 11 Déc 2017
Total = 0;
not understanding why its working
for z = 1:300
Total = Total + z
if mod(z,50)==0
disp(Total)
end
end
  1 commentaire
James Tursa
James Tursa le 11 Déc 2017
Put a semi-colon at the end of a line to suppress the screen output. E.g.,
Total = Total + z;

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by