Programming with JavaScript

⌘K
  1. Home
  2. Docs
  3. Programming with JavaScri...
  4. Example Programs
  5. Sum of First Even Numbers

Sum of First Even Numbers

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

Loading

Views: 43

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments