I am requested to do a sum of the first fifty positive even numbers by using For
loop.
The For
loop that I created is
For[natNum = 2, natNum <= 50, natNum = natNum + 2, Print[natNum]]
The output of this is a list of numbers from 2 to 50 and they are increasing by 2. Now I need these numbers to be sum so I created
Sum[For[natNum = 2, natNum <= 50, natNum = natNum + 2, Print[natNum]]]
However, it is not a correct input. I am not sure if I should write the Sum
inside the For
loop.
For
loops are almost never a good way to do anything in Mathematica. $\endgroup$Total@Range[2, 2*50, 2]
would be pretty direct. Note also thatPrint
prints its arguments, but does not return them (in fact, it returnsNull
). $\endgroup$For
loop to compute50 * 51
is a pretty bad idea.... $\endgroup$