having trouble with for loops

Having Trouble...
'Sum of N^2 with N = 1 to 1,000 using a for loop'

 Accepted Answer

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

3 Comments

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

Sign in to comment.

More Answers (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)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!