MATLAB Answers

sum of 10-9+8+...+2-1 with results in the middle as follow. (Must use the repetition structure)

2 ビュー (過去 30 日間)
JIYEON LEE
JIYEON LEE 2019 年 11 月 24 日
Commented: Rena Berman 2019 年 12 月 12 日
How can I solve this problem with the loop?
sum of 10-9+8+...+2-1 with results in the middle as follow.
You must use the repetition structure.
10=10
10-9=1
10-9+8=9
.
.
.
10-9+8-7+6-5+4-3+2-1=5
The calculation process should also be shown in the command window.

  3 件のコメント

Walter Roberson
Walter Roberson 2019 年 11 月 24 日
Hint: fprintf() with '%+d' format and no spaces between the elements.
Stephen Cobeldick
Stephen Cobeldick 2019 年 11 月 26 日
Original question at 2019-11-24 01:35:57:
"sum of 10-9+8+...+2-1 with results in the middle as follow with the loop"
sum of 10-9+8+...+2-1 with results in the middle as follow by using loop
10=10
10-9=1
10-9+8=9
.
.
.
10-9+8-7+6-5+4-3+2-1=5
Answer:
Revised question at 2019-11-24 02:31:23:
How can I solve this problem with the loop?
sum of 10-9+8+...+2-1 with results in the middle as follow.
You must use the repetition structure.
10=10
10-9=1
10-9+8=9
.
.
.
10-9+8-7+6-5+4-3+2-1=5
The calculation process should also be shown in the command window.
Revised question at 2019-11-24 06:14:08:
"How can I solve this problem?"
Sum of 10-9+...+2-1

サインイン to comment.

件の回答 (1)

David Hill
David Hill 2019 年 11 月 24 日
m=10;
[sprintf('%d',m),sprintf('=%d',sum(m))]
x=[];
for k=9:-1:1
x=[x,k*(-1)^k];
[sprintf('%d',m),sprintf('%+d',x),sprintf('=%d',10+sum(x))]
end

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。

タグ


Translated by