having trouble with for loops

3 views (last 30 days)
Having Trouble...
'Sum of N^2 with N = 1 to 1,000 using a for loop'

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 13 Feb 2014
out = 1;
for ii = 2:1000
out = out + ii^2;
end
  3 Comments
Thomas
Thomas on 13 Feb 2014
what answer do u expect?
Joseph
Joseph on 13 Feb 2014
i got it thanks

Sign in to comment.

More Answers (2)

Roger Stafford
Roger Stafford on 13 Feb 2014
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
Alonso Mukendi Kalonji on 29 Oct 2022
a = 1;
n= 1000;
for i = 1: n
a = a+i^2
end
disp(a)

Categories

Find more on Loops and Conditional Statements 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!