LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to find the sum of the even numbers of an integer

I want to write a program that gets a user-input integer, and adds up the even numbers between 0 and that integer. So if 8 is inputed, the program returns 20 (=2+4+6+8). If 7, the program returns 12=(2+4+6). I know i need to use a loop, but not sure how to make it use the even numbers and find the sum 

Message 1 of 10
(9,493 Views)

Write down in words on a piece of paper how you'd do it.

 

A tip for LabVIEW is to use the Quotient & Remainder function.  If you use that to divide a number by 2, it is even if the remainder is =0.

 

Another though,  divide by 2.  Take the quotient and add up all the values.  Then multiply that by 2.

Message 2 of 10
(9,486 Views)

Its Seems to be your homework, As suggested by ravens

Study about the behaviour of following Functions

R&Q (If R=0 Even)

For Loop(to Run for Given Integer times)

Conditional Indexing in for Loop (To index into array only if R=0)

Arrays(To Add Array Elements )

 

----------------------------------------------------------------------------------------------------------------
Palanivel Thiruvenkadam | பழனிவேல் திருவெங்கடம்
LabVIEW™ Champion |Certified LabVIEW™ Architect |Certified TestStand Developer

Kidlin's Law -If you can write the problem down clearly then the matter is half solved.
-----------------------------------------------------------------------------------------------------------------
Message 3 of 10
(9,466 Views)

I would use Quotient & Remainder to get the number of loop iterations needed.  Then use a FOR loop with a shift register to keep the sum in.  So each iteration you add (i+1)*2.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 4 of 10
(9,418 Views)

@crossrulz wrote:

I would use Quotient & Remainder to get the number of loop iterations needed.  Then use a FOR loop with a shift register to keep the sum in.  So each iteration you add (i+1)*2.


@just Implementation is mine (Idea belongs to you @ Crossrulz)

AddEvenNumbers.png

----------------------------------------------------------------------------------------------------------------
Palanivel Thiruvenkadam | பழனிவேல் திருவெங்கடம்
LabVIEW™ Champion |Certified LabVIEW™ Architect |Certified TestStand Developer

Kidlin's Law -If you can write the problem down clearly then the matter is half solved.
-----------------------------------------------------------------------------------------------------------------
Message 5 of 10
(9,405 Views)

Another way to do this.

 

Sum even nembers.png

Ben64

Message 6 of 10
(9,396 Views)

Guys, this is an obvious homework.  Do not do the work for them!  They will never learn otherwise.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 7 of 10
(9,391 Views)

No loop needed, just do the math!

(Hint: Search for the little Gauß)

 

Greetings from Germany
Henrik

LV since v3.1

“ground” is a convenient fantasy

'˙˙˙˙uıɐƃɐ lɐıp puɐ °06 ǝuoɥd ɹnoʎ uɹnʇ ǝsɐǝld 'ʎɹɐuıƃɐɯı sı pǝlɐıp ǝʌɐɥ noʎ ɹǝqɯnu ǝɥʇ'


Message 8 of 10
(9,387 Views)

Hi Pala,

 

two more simple options:

check.png

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 9 of 10
(9,381 Views)

Solve for the formula, then use an Expression Node.

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 10 of 10
(9,193 Views)