DEV Community

Md. Shariful Islam
Md. Shariful Islam

Posted on

Addition of n successive integers.

I want to determine n successive integer numbers where the value of n is taken from an input box that means taken from user input. How can I write the code. Please give a solution. Advanced Thanks.

Top comments (3)

Collapse
 
saileshbro profile image
Sailesh Dahal

if you want to get the sum of n successive numbers, say 1,2,3,4,5,6 here n=6 then you can return (n)(n+1)/2 which in this case will be 21.
Say you want to get the sum of n numbers starting from the number a and ending at b, and the count of numbers is n, then you can do (a+b)(n)/2,
for example, 5,6,7,8,9 will give (5)(5+9)/2

Collapse
 
sharif1014 profile image
Md. Shariful Islam

I have seen your comment but I don't want to understand that actually I wanted the JavaScript code where the use give the value of n through the browser using an input box.

Collapse
 
saileshbro profile image
Sailesh Dahal
function getSumUpto(n){
    return (n+1)*n*0.5;
}
Enter fullscreen mode Exit fullscreen mode