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

1 visualización (últimos 30 días)
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 comentarios
Stephen23
Stephen23 el 26 de Nov. de 2019
Editada: Stephen23 el 26 de Nov. de 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

Iniciar sesión para comentar.

Respuestas (1)

David Hill
David Hill el 24 de Nov. de 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

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by