Find the sum of all multiples of 2 less than 10000

We are finding all the multiples of 2 less than 10000 so first it's a good idea to iterate over 10000.

For this we use a loop such as a for loop to check each number from 0 to 10000.

Then we need to check if the number we are iterating over is a multiple of 2.  For this we need an if statement. One way to find a multiple of 2 is to use the modulus operator '%'.  The modulus evaluates the remainder of two numbers for example 4 % 2 would evaluate to 0 as there is no remainder.  This can be used to find the numbers that are multiples of 2. 

Finally we need to sum these multiples.  To store the sum we need to create a variable. Every time a number we are iterating over satisifies our if condition we must add it on top of whatever is already stored in our sum variable.

The final code might look something like this:

var sum = 0, i

for (i = 0; i < 10000; i++) {
    if (i % 2 == 0) {
      sum = sum + i

N.b notice that in standard js syntax no semicolons are used as line terminators and indentation is two spaces.

Tom B. GCSE Computing tutor, GCSE Maths tutor, Mentoring HTML & CSS t...

10 months ago

Answered by Tom, a Mentoring Javascript tutor with MyTutor

Still stuck? Get one-to-one help from a personally interviewed subject specialist


About the author

Tom B.

Currently unavailable: for new students

Degree: Electronic Engineering (Masters) - York University

Subjects offered:Javascript, Maths


“Hi I'm an Electronic Engineering student at the University of York.  I love using my technical skills in order to work on creative new projects.  My specialty is software programming, which I have enjoyed at an academic and proffesion...”

MyTutor guarantee

|  1 completed tutorial

You may also like...

Posts by Tom

Explain what a pseudo class selector is

Find the sum of all multiples of 2 less than 10000

Solve 4x + 6 = 3

What is the difference between a data structure and a data type?

Other Mentoring Javascript questions

How to check if value exists in an object in JavaScript?

Find the sum of all multiples of 2 less than 10000

What is the importance of javascript in the web?

How to sum every number in an array of numbers.

View Mentoring Javascript tutors

We use cookies to improve your site experience. By continuing to use this website, we'll assume that you're OK with this. Dismiss