Need help with a While loop: Summation
이전 댓글 표시
% Write a while loop that assigns summedValue with the sum of all values from 1 to userNum.
% Assume userNum is always greater than or equal to 1
function summedValue = SummationWithLoop(userNum)
% Summation of all values from 1 to userNum
summedValue = 0;
i = 1;
while (i<=userNum);
i=i+1
SummationWithLoop
% Write a while loop that assigns summedValue with the
% sum of all values from 1 to userNum
end
댓글 수: 2
Lily Pestorius
2021년 4월 17일
Cris LaPierre
2021년 4월 17일
Did you mean to create a recursive function?
답변 (1개)
You need this line:
summedValue = summedValue + i;
There is no need to call this function recursively, so omit the line
SummationWithLoop
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!