Sum of first 100 integers?

조회 수: 20 (최근 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 CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by