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

1 view (last 30 days)
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 Comments
Stephen23
Stephen23 on 26 Nov 2019
Edited: Stephen23 on 26 Nov 2019
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

Sign in to comment.

Answers (1)

David Hill
David Hill on 24 Nov 2019
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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!