Info
此问题已关闭。 请重新打开它进行编辑或回答。
New to matlab, need help with problem..
1 次查看(过去 30 天)
显示 更早的评论
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)
0 个评论
回答(2 个)
James Tursa
2017-12-11
编辑:James Tursa
2017-12-11
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
0 个评论
Mohammed Safwat
2017-12-11
编辑:James Tursa
2017-12-11
1 个评论
James Tursa
2017-12-11
Put a semi-colon at the end of a line to suppress the screen output. E.g.,
Total = Total + z;
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!