Hi,I want to compute the sum 2 + 4 + 6 + 8 + 10 + : : : + 100 by using For loop . Can anyone here thankfully help me out?

4 Kommentare

James Tursa
James Tursa am 5 Okt. 2017
What have you done so far? What specific problems are you having with your code?
F.O
F.O am 5 Okt. 2017
Bearbeitet: James Tursa am 5 Okt. 2017
Actually I am asked to do the sum in to ways and the second one is For loop which i dont understand first way is
a=sum([2:2:100])
I tried the way you recommended but get nothing when i run it except that i get
k=100
total=50
total = 0;
for k=2:2:100;
total = total + 1;
end
James Tursa
James Tursa am 5 Okt. 2017
Bearbeitet: James Tursa am 5 Okt. 2017
Change
total = total + 1;
to
total = total + k; % <-- You need to add the index variable k, not 1 every time
F.O
F.O am 6 Okt. 2017
I have changed it and got correct answer. Thanks

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

James Tursa
James Tursa am 5 Okt. 2017

0 Stimmen

The general outline of your for loop would be:
total = 0;
for k=start_value:increment:end_value % <-- you fill in the start_value, increment, and end_value
total = total + something; % <-- you fill in the something
end

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Produkte

Gefragt:

F.O
am 5 Okt. 2017

Kommentiert:

F.O
am 6 Okt. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by