Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Help with calculating sums

Asked by O on 14 Nov 2013
Latest activity Answered by O on 19 Nov 2013

Write a program that receives an integer (n) and then calculate the sum of integers 1 to n (exp. 1 + 2 + 3 + ….. + n) using for loop

so confused :(

1 Comment

Roger Stafford on 14 Nov 2013

As a last desperate measure you could simply write:

 the_sum = n*(n+1)/2;

as Gauss is said to have correctly done in his primary school years.

O

Products

No products are associated with this question.

2 Answers

Answer by Image Analyst on 14 Nov 2013
Accepted answer

Use inputdlg() to get n - look in the help for examples. Then use a for loop. Here's a hint to get you started.

theSum = 0;
for k = 1 : n
  % your code goes here.
  theSum = theSum + ........
end

I don't know what you mean by exp. Do you want e to the power? Like exp(1) + exp(2) + exp(3) + .... OR do you want exp(1+2+3+.....????

0 Comments

Image Analyst
Answer by O on 19 Nov 2013

It means (example) ... i thought it meant exponential too. Thanks a million!

0 Comments

O

Contact us