having trouble with for loops

 采纳的回答

out = 1;
for ii = 2:1000
out = out + ii^2;
end

3 个评论

SumN=0; % initializes N^2
for N=1:1000
SumN = SumN + N^2;
end
Still not giving me correct answer...Don't know what I'm doing wrong
what answer do u expect?
i got it thanks

请先登录,再进行评论。

更多回答(2 个)

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);
a = 1;
n= 1000;
for i = 1: n
a = a+i^2
end
disp(a)

类别

在 帮助中心 和 File 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