Given a number **n**. The problem is to find the sum of first **n** even numbers.

Examples:

Input : n = 4 Output : 20 Sum of first4even numbers = (2 + 4 + 6 + 8) = 20 Input : n = 20 Output : 420

**Naive Approach:** Iterate through the first **n** even numbers and add them.

## C++

`// C++ implementation to find sum of ` `// first n even numbers ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// function to find sum of ` `// first n even numbers ` `int` `evenSum(` `int` `n) ` `{ ` ` ` `int` `curr = 2, sum = 0; ` ` ` ` ` `// sum of first n even numbers ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` `sum += curr; ` ` ` ` ` `// next even number ` ` ` `curr += 2; ` ` ` `} ` ` ` ` ` `// required sum ` ` ` `return` `sum; ` `} ` ` ` `// Driver program to test above ` `int` `main() ` `{ ` ` ` `int` `n = 20; ` ` ` `cout << ` `"Sum of first "` `<< n ` ` ` `<< ` `" Even numbers is: "` `<< evenSum(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find sum ` `// of first n even numbers ` `import` `java.util.*; ` `import` `java.lang.*; ` ` ` `public` `class` `GfG{ ` ` ` ` ` `// function to find sum of ` ` ` `// first n even numbers ` ` ` `static` `int` `evenSum(` `int` `n) ` ` ` `{ ` ` ` `int` `curr = ` `2` `, sum = ` `0` `; ` ` ` ` ` `// sum of first n even numbers ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) { ` ` ` `sum += curr; ` ` ` ` ` `// next even number ` ` ` `curr += ` `2` `; ` ` ` `} ` ` ` ` ` `// required sum ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// driver function ` ` ` `public` `static` `void` `main(String argc[]) ` ` ` `{ ` ` ` `int` `n = ` `20` `; ` ` ` `System.out.println(` `"Sum of first "` `+ n + ` ` ` `" Even numbers is: "` `+ ` ` ` `evenSum(n)); ` ` ` `} ` ` ` `} ` ` ` `// This code is contributed by Prerna Saini ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find sum of ` `# first n even numbers ` ` ` `# function to find sum of ` `# first n even numbers ` `def` `evensum(n): ` ` ` `curr ` `=` `2` ` ` `sum` `=` `0` ` ` `i ` `=` `1` ` ` ` ` `# sum of first n even numbers ` ` ` `while` `i <` `=` `n: ` ` ` `sum` `+` `=` `curr ` ` ` ` ` `# next even number ` ` ` `curr ` `+` `=` `2` ` ` `i ` `=` `i ` `+` `1` ` ` `return` `sum` ` ` `# Driver Code ` `n ` `=` `20` `print` `(` `"sum of first "` `, n, ` `"even number is: "` `, ` ` ` `evensum(n)) ` ` ` `# This article is contributed by rishabh_jain ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find sum ` `// of first n even numbers ` `using` `System; ` ` ` `public` `class` `GfG { ` ` ` ` ` `// function to find sum of ` ` ` `// first n even numbers ` ` ` `static` `int` `evenSum(` `int` `n) ` ` ` `{ ` ` ` `int` `curr = 2, sum = 0; ` ` ` ` ` `// sum of first n even numbers ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` `sum += curr; ` ` ` ` ` `// next even number ` ` ` `curr += 2; ` ` ` `} ` ` ` ` ` `// required sum ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// driver function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 20; ` ` ` ` ` `Console.WriteLine(` `"Sum of first "` `+ n ` ` ` `+ ` `" Even numbers is: "` `+ evenSum(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt-m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation to find sum of ` `// first n even numbers ` ` ` `// function to find sum of ` `// first n even numbers ` `function` `evenSum(` `$n` `) ` `{ ` ` ` `$curr` `= 2; ` ` ` `$sum` `= 0; ` ` ` ` ` `// sum of first n even numbers ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$n` `; ` `$i` `++) { ` ` ` `$sum` `+= ` `$curr` `; ` ` ` ` ` `// next even number ` ` ` `$curr` `+= 2; ` ` ` `} ` ` ` ` ` `// required sum ` ` ` `return` `$sum` `; ` `} ` ` ` `// Driver program to test above ` ` ` ` ` `$n` `= 20; ` ` ` `echo` `"Sum of first "` `.` `$n` `.` `" Even numbers is: "` `.evenSum(` `$n` `); ` ` ` `// this code is contributed by mits ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Sum of first 20 Even numbers is: 420

**Time Complexity:** O(n).

**Efficient Approach:** By applying the formula given below.

Sum of first n even numbers = n * (n + 1).

**Proof:**

Sum of firstnterms of an A.P.(Arithmetic Progression) =(n/2) * [2*a + (n-1)*d].....(i) where,ais the first term of the series anddis the difference between the adjacent terms of the series. Here, a = 2, d = 2, applying these values to eq.(i), we get Sum = (n/2) * [2*2 + (n-1)*2] = (n/2) * [4 + 2*n - 2] = (n/2) * (2*n + 2) =n * (n + 1)

## C++

`// C++ implementation to find sum of ` `// first n even numbers ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to find sum of ` `// first n even numbers ` `int` `evenSum(` `int` `n) ` `{ ` ` ` `// required sum ` ` ` `return` `(n * (n + 1)); ` `} ` ` ` `// Driver program to test above ` `int` `main() ` `{ ` ` ` `int` `n = 20; ` ` ` `cout << ` `"Sum of first "` `<< n ` ` ` `<< ` `" Even numbers is: "` `<< evenSum(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find sum ` `// of first n even numbers ` `import` `java.util.*; ` `import` `java.lang.*; ` ` ` `public` `class` `GfG{ ` ` ` ` ` `// function to find sum of ` ` ` `// first n even numbers ` ` ` `static` `int` `evenSum(` `int` `n) ` ` ` `{ ` ` ` `// required sum ` ` ` `return` `(n * (n + ` `1` `)); ` ` ` `} ` ` ` ` ` `// driver function ` ` ` `public` `static` `void` `main(String argc[]) ` ` ` `{ ` ` ` `int` `n = ` `20` `; ` ` ` `System.out.println(` `"Sum of first "` `+ n + ` ` ` `" Even numbers is: "` `+ ` ` ` `evenSum(n)); ` ` ` `} ` `} ` ` ` ` ` `// This code is contributed by Prerna Saini ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find ` `# sum of first n even numbers ` ` ` `# function to find sum of ` `# first n even numbers ` `def` `evensum(n): ` ` ` `return` `n ` `*` `(n ` `+` `1` `) ` ` ` `# Driver Code ` `n ` `=` `20` `print` `(` `"sum of first"` `, n, ` `"even number is: "` `, ` ` ` `evensum(n)) ` ` ` `# This article is contributed by rishabh_jain ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find sum ` `// of first n even numbers' ` `using` `System; ` ` ` `public` `class` `GfG { ` ` ` ` ` `// function to find sum of ` ` ` `// first n even numbers ` ` ` `static` `int` `evenSum(` `int` `n) ` ` ` `{ ` ` ` ` ` `// required sum ` ` ` `return` `(n * (n + 1)); ` ` ` `} ` ` ` ` ` `// driver function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 20; ` ` ` ` ` `Console.WriteLine(` `"Sum of first "` `+ n ` ` ` `+ ` `" Even numbers is: "` `+ evenSum(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation ` `// to find sum of ` `// first n even numbers ` ` ` `// function to find sum of ` `// first n even numbers ` `function` `evenSum(` `$n` `) ` `{ ` ` ` `// required sum ` ` ` `return` `(` `$n` `* (` `$n` `+ 1)); ` `} ` ` ` `// Driver Code ` `$n` `= 20; ` `echo` `"Sum of first "` `, ` `$n` `, ` ` ` `" Even numbers is: "` `, ` ` ` `evenSum(` `$n` `); ` ` ` `// This code is contributed ` `// by akt_mit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Sum of first 20 Even numbers is: 420

**Time Complexity: **O(1).

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sum of cubes of first n even numbers
- Sum of square of first n even numbers
- Sum of fourth power of first n even natural numbers
- Sum of range in a series of first odd then even natural numbers
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Average of even numbers till a given even number
- Count ways to express even number ‘n’ as sum of even integers
- Average of first n even natural numbers
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Find all even length binary sequences with same sum of first and second half bits | Iterative
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Difference between sum of the squares of first n natural numbers and square of sum
- Sum of sum of all subsets of a set formed by first N natural numbers
- Difference between Sum of Cubes and Sum of First N Natural Numbers
- Sum of sum-series of first N Natural numbers
- Check if a given number can be expressed as pair-sum of sum of first X natural numbers
- Print even positioned nodes of even levels in level order of the given binary tree
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Check if a number has an odd count of odd divisors and even count of even divisors
- Rearrange array such that all even-indexed elements in the Array is even

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.