Sum of first 100 integers?

64 ビュー (過去 30 日間)
Ian
Ian 2014 年 2 月 3 日
コメント済み: Jan 2022 年 3 月 15 日
Write a script that uses a for loop to calculate the sum of the first 100 integer numbers

採用された回答

Mischa Kim
Mischa Kim 2014 年 2 月 3 日
編集済み: Mischa Kim 2014 年 2 月 3 日
my_sum = 0;
for ii = 1:100
my_sum = my_sum + ii;
end
  1 件のコメント
Lukeus Perez
Lukeus Perez 2017 年 3 月 3 日
You're the best

サインインしてコメントする。

その他の回答 (4 件)

Sean de Wolski
Sean de Wolski 2014 年 2 月 3 日
for ii = 100
s = sum(1:ii);
end

Roger Stafford
Roger Stafford 2014 年 2 月 3 日
編集済み: Roger Stafford 2014 年 2 月 3 日
Or you could emulate the mathematical genius, Karl Friedrich Gauss, who is said to have done it this way at the age of ten:
s = (100+1)+(99+2)+(98+3)+...+(51+50) = 50*101 = 5050
  2 件のコメント
Ian
Ian 2014 年 2 月 3 日
Gotta love good ol' Gauss!
Jan
Jan 2021 年 3 月 4 日
This means as code:
n = 100;
s = (n * (n + 1)) / 2

サインインしてコメントする。


Mayur Gaikwad
Mayur Gaikwad 2017 年 12 月 7 日
Could someone write the script for matlab mobile version for the sum of the squares of first 100 natural numbers.. !
  1 件のコメント
Jan
Jan 2017 年 12 月 7 日
Yes. Simply add a "^2" in Mischa's answer.
Please do not inject a new question as an answer to an existing thread. Such thread-hijacking confuses the readers, because it is not clear anymore, to which question an answer belongs.
Because this sounds like a homework, let me encourage you to try to solve this by your own.

サインインしてコメントする。


Bhanuprasad Akula
Bhanuprasad Akula 2021 年 3 月 3 日
sum(1:100)
  1 件のコメント
Jan
Jan 2022 年 3 月 15 日
"uses a for loop"...

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by