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

New to MATLAB?

how to use a while loop to solve this?

Asked by Jack

Jack (view profile)

on 10 Sep 2013

Hello, I need help with while loop please help Use a while loop to add all of the even numbers up: 2 + 4 + 6 + 8 + · · · until the sum exceeds 5 million. What is the actual sum? How many even numbers were added?

0 Comments

Jack

Jack (view profile)

Products

No products are associated with this question.

2 Answers

Answer by Azzi Abdelmalek

Azzi Abdelmalek (view profile)

on 10 Sep 2013
Edited by Azzi Abdelmalek

Azzi Abdelmalek (view profile)

on 10 Sep 2013
Accepted answer

An example how the while loop works

If we have to do 1+2+3+4+...+10

s=0;
k=0;
while  k<10
  k=k+1;
  s=s+k
end

Try to adapt this to your problem

0 Comments

Azzi Abdelmalek

Azzi Abdelmalek (view profile)

Answer by Jan Simon

Jan Simon (view profile)

on 10 Sep 2013

For homework questions it is recommended to show, what you have tried already. Then the forum can help to solve a specific problem.

Note that Gauss has found a formula to solve the addition in several seconds without a computer. You can re-order the terms of his formula to solve your problem also.

0 Comments

Jan Simon

Jan Simon (view profile)

Contact us