How to calculate the sum of the first N natural numbers in Node.js

1 Answer

0 votes
// In mathematics, the natural numbers are 1, 2, 3, 4, and so on
// 1 + 2 + 3 + 4 + ... + N

let n = 13, sum = 0;
 
sum = n / 2 * (n + 1);
         
console.log("The sum is: " + sum);



/*
run:  

The sum is: 91

*/

 



answered Nov 12, 2024 by avibootz

Related questions

2 answers 236 views
2 answers 230 views
2 answers 197 views
2 answers 517 views
2 answers 213 views
2 answers 196 views
...