Write a JavaScript program to calculate sum of 1 to n numbers
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Practical-14</title>
</head>
<body>
<script>
var num = prompt("Enter the number ");
var sum = 0;
for (let i = 0; i <= num; i++) {
sum = sum + i;
}
alert(`Sum of the 1 to ${num} numbers is :` + sum);
</script>
</body>
</html>
Comments
Post a Comment