JavaScript find the sum of all odd numbers smaller than a number

JavaScript: find the sum of all odd numbers smaller than a given number:

To find the sum of all odd numbers smaller than one given number, we need to iterate over the numbers smaller than the number. For each number, we need to check if it is odd or not. If an odd number is found, we will add it to a sum variable. This sum variable will hold the total sum of all the odd numbers and it will be initialized as 0.

How to check if a number is odd or not:

A number is called an odd number if it is not perfectly divisible by 2. For example, 3, 5, 7 etc. are odd numbers.

We can use the modulo operator, % to find the remainder of a division. For example, the operation a%b will return the remainder of a/b for two numbers a and b. If the number a is odd, a%2 will return 1. We can use this operator in the programs.

Method 1: Find the sum of all odd numbers with a for loop in Javascript:

The following program uses a for loop to find the sum of all odd numbers smaller than a given number:

function isOdd(n) {
  return Boolean(n % 2);
}

function findSum(no) {
  let sum = 0;

  for (var i = 0; i < no; i++) {
    if (isOdd(i)) {
      sum += i;
    }
  }
  return sum;
}

console.log(findSum(100));

Download it on Github

Explanation :

  1. We have two functions in this program, findSum and isOdd.
  2. The findSum function is the main function to find the sum. It takes one number as the argument and returns the sum of all odd numbers smaller than the argument number.
  3. The isOdd function takes one number as its parameter and returns one Boolean value. It finds the value of n%2 and converts this value to Boolean. For odd numbers, it will be true as the value of Boolean(1) is true.
  4. The sum variable is used to hold the sum of all odd numbers.
  5. The for loop runs from i = 0 to i < no and finds out all the odd numbers. The isOdd function is used to check if the number is odd or not.
  6. If the value of i is odd, it is added to the current value of sum.
  7. Finally, the value of sum is returned from the findSum function.

In this example, we are finding out the sum of all odd numbers smaller than 100, but we can use this same program to find out all odd numbers smaller than any user-given number.

If you run this program, it will print 2500 as the output.

Method 2: Find the sum of all odd numbers with a while loop in Javascript:

Similar to the previous program, we can also use a while loop to find the sum. The while loop checks one condition and runs the body of the loop until the condition is true. The syntax of the while loop is:

while(condition)
{

}

The following program uses a while loop to find the sum of all odd numbers:

function isOdd(n) {
  return Boolean(n % 2);
}

function findSum(no) {
  let sum = 0,
    i = 0;

  while (i < no) {
    if (isOdd(i)) {
      sum += i;
    }
    i++;
  }
  return sum;
}

console.log(findSum(100));

Download it on Github

The while loop will run until the condition i < no is true. We are incrementing the value of i at the end of the loop. It will print similar results.

Method 3: Find the sum of all odd numbers by jumping between the numbers:

Another way to find the sum of all odd numbers is by jumping between the odd numbers. We can start the loop from 1 and increment the variable by 2 at the end of each iteration. It will always point to an odd number if we increment the value by 2. Also, no need to check if a value is odd or not.

This approach can be used with for or while loop:

Example with for loop:

function findSum(no) {
  let sum = 0;

  for (var i = 1; i < no; i += 2) {
    sum += i;
  }
  return sum;
}

console.log(findSum(100));

Download it on Github

The above program starts from i = 1 and on each step, it increments its value by 2. It adds all the values to the sum variable.

JavaScript find sum of odd numbers

It will print 2500.

Example with while loop:

Similar to the previous example, we can also use a while loop:

function findSum(no) {
  let sum = 0,
    i = 1;

  while (i < no) {
    sum += i;
    i += 2;
  }
  return sum;
}

console.log(findSum(100));

Download it on Github

It will print similar output.

You might also like: