Table of Contents
show
Example 20
Write a JavaScript to find the sum of first n even numbers and display the result
const prompt = require("prompt-sync")();
let n = prompt("Enter the number : ");
let sum = 0;
for (let i = 1; i <= n; i++) {
sum += 2 * i;
}
console.log(`sum of first ${n} even numbers is : ${sum}`);
Output
Views: 43