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
Lily Pestorius 2021 年 4 月 17 日
Sorry I forgot to include what I am confused about. I am a little confused about which variables I should be putting where, I know that if SummationWithLoop is 5 then the answer should be 15 (1+2+3+4+5=15).
Cris LaPierre
Cris LaPierre 2021 年 4 月 17 日
Did you mean to create a recursive function?

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

回答 (1 件)

Jan
Jan 2021 年 4 月 17 日
編集済み: Jan 2021 年 4 月 17 日

0 投票

You need this line:
summedValue = summedValue + i;
There is no need to call this function recursively, so omit the line
SummationWithLoop

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2021 年 4 月 17 日

編集済み:

Jan
2021 年 4 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by