Find sum of of n numbers using loop?

//Expected output

Enter the number:5

Sum of numbers:15

Find sum of of n numbers using loop?

//Expected output

Enter the number:5

Sum of numbers:15

1 Like

var n=parseInt(prompt("Enter a number: "));

let s=0;

for(let i=1;i<=n;i++){

s+=i;

}

console.log("Sum of numbers: ",s);

2 Likes

var n=prompt(‘Enter the number’);

var sum=0;

for(var i=0;i<n;i++){

sum=sum+i;

}

document.write(‘Sum is:’+sum);

1 Like