JavaScript program to find the sum of all even numbers below another number:
In this JavaScript program, we will learn how to find the sum of all even numbers smaller than a given number. It will find the sum of all even numbers from 1 to that number. A number is called an even number if it is perfectly divisible by 2.
We will use a loop to iterate through the numbers from 1 to that number and for each number, we will check if it is even or not with a separate function. If it is even, we will add it to a separate variable that will hold the final sum. At the end of the program, it will print the value of the final sum.
Method 1: By using a for
loop:
With this approach, we will use a for
loop to find the sum of the even numbers smaller than another number. Below is the complete JavaScript program:
function isEven(n) {
return n % 2 == 0;
}
function findSum(no) {
let sum = 0;
for (var i = 1; i <= no; i++) {
if (isEven(i)) {
sum += i;
}
}
return sum;
}
console.log(findSum(100));
Download the program on GitHub
Here,
- The
isEven
method is used to check if a number is even or not. It checks if the number is perfectly divisible by 2. It uses the modulo operator,%
. The modulo operator returns the remainder of a division operation. Then%2
operation will return the remainder ofn/2
. So, ifn
is even, the operationn%2
will return 0. - The
findSum
method takes the number as its parameter and returns the sum of all even numbers from 1 to that number. - It uses one
for
loop that runs from 1 tono
and for each number, it checks if it is even or not. If it is even, it adds that value to thesum
variable. Thesum
variable is initialized as 0. So, once thefor
loop ends, it will hold the total sum of all even numbers in this range.
If I execute the above program, it will give the below output:
2550
Method 2: By using a while
loop:
We can also use a while
loop to solve it. The below program shows how it works:
function isEven(n) {
return n % 2 == 0;
}
function findSum(no) {
let sum = 0;
let i = 1;
while (i <= no) {
if (isEven(i)) {
sum += i;
}
i++;
}
return sum;
}
console.log(findSum(100));
Download the program on GitHub
- It is similar to the previous program.
- The only difference is that we are using a
while
loop instead of afor
loop. It runs fromi=1
toi=no
and on each iteration, it adds the value ofi
to thesum
variable if it is even.
If you run this program, it will print similar output.
Method 3: By using a do...while
loop:
The do...while
loop works similar to a while
loop. It executes the code in the do
block and checks the condition in the while
block. The following program shows how to use a do...while
loop to find the sum of even numbers:
function isEven(n) {
return n % 2 == 0;
}
function findSum(no) {
let sum = 0;
let i = 1;
do {
if (isEven(i)) {
sum += i;
}
i++;
} while (i <= no);
return sum;
}
console.log(findSum(100));
Download the program on GitHub
Method 4: By jumping between the numbers:
We can jump between the even numbers in a loop and add all the numbers to get the required sum. The loop can start from 2 and on each step, it can move to the next even number. This approach is faster than the previous examples:
function findSum(no) {
let sum = 0;
for (var i = 2; i <= no; i += 2) {
sum += i;
}
return sum;
}
console.log(findSum(100));
Download the program on GitHub
The for
loop starts from i = 2
and on each step, it increments the value of i by 2. It adds the values of i
to the sum
variable in the loop. The sum
variable will hold the sum of all even numbers. It will print the same output.