having trouble with for loops

1 次查看(过去 30 天)
Having Trouble...
'Sum of N^2 with N = 1 to 1,000 using a for loop'

采纳的回答

Andrei Bobrov
Andrei Bobrov 2014-2-13
out = 1;
for ii = 2:1000
out = out + ii^2;
end
  3 个评论
Thomas
Thomas 2014-2-13
what answer do u expect?

请先登录,再进行评论。

更多回答(2 个)

Roger Stafford
Roger Stafford 2014-2-13
Here's a way to check your answer:
n = 1000;
s = n*(n+1)*(2*n+1)/6;
This gives you the same value as
s = sum((1:n).^2);

Alonso Mukendi Kalonji
a = 1;
n= 1000;
for i = 1: n
a = a+i^2
end
disp(a)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by