JavaScript Program to Find the Sum of Natural Numbers
Natural numbers are positive integers starting from 1 (e.g., 1, 2, 3, ...). The sum of the first n natural numbers can be calculated using loops, recursion, or a direct formula. For example:
-
The sum of the first 5 natural numbers (1, 2, 3, 4, 5) is .
In this tutorial, you will learn how to find the sum of natural numbers using various approaches in JavaScript.
JavaScript Programs to Find the Sum of Natural Numbers Using for Loop
This program uses a for loop to calculate the sum of the first n natural numbers.
Code
function sumUsingForLoop(n) {
let sum = 0;
for (let i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
// Test the function
const n = 5;
console.log(`The sum of the first ${n} natural numbers is:`, sumUsingForLoop(n));
Output
The sum of the first 5 natural numbers is: 15
Explanation
-
A variable sum is initialized to 0.
-
A for loop iterates from 1 to n, adding each value to sum.
-
The final sum is returned.
JavaScript Programs to Find the Sum of Natural Numbers Using Formula
The sum of the first n natural numbers can be calculated directly using the formula:
Code
Sum = n * (n + 1) / 2
This method is more efficient than using loops.
function sumUsingFormula(n) {
return (n * (n + 1)) / 2;
}
// Test the function
console.log(`The sum of the first ${n} natural numbers is:`, sumUsingFormula(n));
Output
The sum of the first 5 natural numbers is: 15
Explanation
-
The formula directly computes the sum in constant time.
-
It avoids the need for iteration, making it more efficient for large values of n.
JavaScript Programs to Find the Sum of Natural Number Using Recursion
This program calculates the sum recursively by adding n to the sum of the first n-1 natural numbers.
Code
function sumUsingRecursion(n) {
if (n === 0) return 0; // Base case
return n + sumUsingRecursion(n - 1); // Recursive case
}
// Test the function
console.log(`The sum of the first ${n} natural numbers is:`, sumUsingRecursion(n));
Output
The sum of the first 5 natural numbers is: 15
Explanation
-
The base case (n === 0) stops the recursion when n reaches 0.
-
The function adds n to the result of the recursive call for n-1.
Sum of Natural Number in JavaScript Using while Loop
This program uses a while loop to calculate the sum of the first n natural numbers.
Code
function sumUsingWhileLoop(n) {
let sum = 0;
let i = 1;
while (i <= n) {
sum += i;
i++;
}
return sum;
}
// Test the function
console.log(`The sum of the first ${n} natural numbers is:`, sumUsingWhileLoop(n));
Output
The sum of the first 5 natural numbers is: 15
Explanation
-
A variable sum is initialized to 0, and i starts from 1.
-
The while loop adds i to sum as long as i is less than or equal to n.
-
The loop increments i after each iteration.
Sum of Natural Numbers in JavaScript for a Range of Numbers
This program calculates the sum of all natural numbers between two given numbers.
Code
function sumForRange(start, end) {
let sum = 0;
for (let i = start; i <= end; i++) {
sum += i;
}
return sum;
}
// Test the function
const start = 3;
const end = 7;
console.log(`The sum of natural numbers from ${start} to ${end} is:`, sumForRange(start, end));
Output
The sum of natural numbers from 3 to 7 is: 25
Explanation
-
A for loop iterates from start to end, adding each value to sum.
-
The final sum is returned.
Concepts Used in Above Programs
Iteration
-
for and while loops are used to iterate through numbers and compute their sum.
Recursion
-
A recursive function simplifies the calculation by breaking it into smaller subproblems.
Mathematical Formula
-
The formula provides an efficient way to calculate the sum without iteration.