Asked by Aaron Moris
on 11 Apr 2011

What is the product if the question is write a program that calculates and prints the sum of the even integers from 2 to 30?

Answer by Florin Neacsu
on 11 Apr 2011

2+4+6+...+30=2(1+2+3+...+15)=2*(15*16)/2=15*16

So 2+4+...+n = floor(n/2)*(floor(n/2)+1).

Regards, Florin

Log in to comment.

Answer by Loginatorist
on 11 Apr 2011

The product would be the sum of the even integers from 2 to 30 ;-).

Seriously, what have you tried so far on this homework problem?

Sean de Wolski
on 11 Apr 2011

You might need some bubble wrap for that.

Paulo Silva
on 11 Apr 2011

I hate when it takes more time to understand what people want than to make their answer!

Log in to comment.

Answer by Paulo Silva
on 11 Apr 2011

v=2:30; sum(v(~mod(v,2)))

Brother_Devil
on 15 Aug 2017 at 2:58

Image Analyst
on 15 Aug 2017 at 3:09

mod(v, 2) will be either 0 or 1. The tilde inverts that, so 0 becomes 1, and 1 becomes 0.

Log in to comment.

Answer by Doug Eastman
on 11 Apr 2011

or

sum(2:2:30)

Paulo Silva
on 11 Apr 2011

Paulo Silva
on 11 Apr 2011

btw that does work and is a good answer for the question (only if the values don't change), +1 vote

Log in to comment.

Answer by Matt Tearle
on 11 Apr 2011

disp(240)

Or, for pedants who claim that evaluation is not the same as calculation,

disp(239+1)

Sean de Wolski
on 11 Apr 2011

This is definitely the most compact and efficient way to do this. +1.

Log in to comment.

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 0 Comments

Log in to comment.