JavaScript Program to Find the Sum of Natural Numbers

bookmark

// program to display the sum of natural numbers

// take input from the user
const number = parseInt(prompt('Enter a positive integer: '));

let sum = 0;

// looping from i = 1 to number
// in each iteration, i is increased by 1
for (let i = 1; i <= number; i++) {
    sum += i;
}

console.log('The sum of natural numbers:', sum);

 

 

 

Output

Enter a positive integer: 100
The sum of natural numbers: 5050