how to use a while loop to solve this?

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?

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 10 日
編集済み: Azzi Abdelmalek 2013 年 9 月 10 日

0 投票

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

その他の回答 (1 件)

Jan
Jan 2013 年 9 月 10 日

0 投票

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.

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by