# Average of first n even natural numbers

Given a number n then Find the Average of first n even natural numbers

Ex.= 2 + 4 + 6 + 8 + 10 + 12 +………+ 2n.

**Examples :**

Input : 7 Output : 8 (2 + 4 + 6 + 8 + 10 + 12 + 14)/7 = 8 Input : 5 Output : 6 (2 + 4 + 6 + 8 + 10)/5 = 6

**Naive Approach:-** In this program iterate the loop , finding total sum of first n even numbers and divided by n.it take 0(N) time.

## C++

`// C++ implementation to find Average ` `// of sum of first n natural even numbers ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to find average of ` `// sum of first n even numbers ` `int` `avg_of_even_num(` `int` `n) ` `{ ` ` ` `// sum of first n even numbers ` ` ` `int` `sum = 0; ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `sum += 2*i; ` ` ` ` ` `// calculating Average ` ` ` `return` `sum/n; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 9; ` ` ` `cout << avg_of_even_num(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// java implementation to find Average ` `// of sum of first n natural even number ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// function to find average of ` ` ` `// sum of first n even numbers ` ` ` `static` `int` `avg_of_even_num(` `int` `n) ` ` ` `{ ` ` ` ` ` `// sum of first n even numbers ` ` ` `int` `sum = ` `0` `; ` ` ` ` ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) ` ` ` `sum += ` `2` `*i; ` ` ` ` ` `// calculating Average ` ` ` `return` `(sum / n); ` ` ` `} ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` ` ` `int` `n = ` `9` `; ` ` ` `System.out.print(avg_of_even_num(n)); ` ` ` ` ` `} ` `} ` ` ` `// this code is contributed by 'vt_m' ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to ` `# find Average of sum of ` `# first n natural even ` `# number ` ` ` `# Function to find average ` `# of sum of first n even ` `# numbers ` `def` `avg_of_even_num(n): ` ` ` ` ` `# sum of first n even ` ` ` `# numbers ` ` ` `sum` `=` `0` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `): ` ` ` `sum` `=` `sum` `+` `2` `*` `i ` ` ` ` ` `# calculating Average ` ` ` `return` `sum` `/` `n ` ` ` `n` `=` `9` `print` `(avg_of_even_num(n)) ` ` ` `# This code is contributed by upendra singh bartwal ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find ` `// Average of sum of first ` `// n natural even number ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// function to find average of ` ` ` `// sum of first n even numbers ` ` ` `static` `int` `avg_of_even_num(` `int` `n) ` ` ` `{ ` ` ` ` ` `// sum of first n even numbers ` ` ` `int` `sum = 0; ` ` ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `sum += 2 * i; ` ` ` ` ` `// calculating Average ` ` ` `return` `(sum / n); ` ` ` `} ` ` ` ` ` `// driver code ` ` ` `public` `static` `void` `Main () { ` ` ` ` ` `int` `n = 9; ` ` ` `Console.Write(avg_of_even_num(n)); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by 'vt_m' ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation to find Average ` `// of sum of first n natural even numbers ` ` ` `// function to find average of ` `// sum of first n even numbers ` `function` `avg_of_even_num(` `$n` `) ` `{ ` ` ` `// sum of first n even numbers ` ` ` `$sum` `= 0; ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$n` `; ` `$i` `++) ` ` ` `$sum` `+= 2 * ` `$i` `; ` ` ` ` ` `// calculating Average ` ` ` `return` `$sum` `/ ` `$n` `; ` `} ` ` ` `// Driver Code ` `$n` `= 9; ` `echo` `(avg_of_even_num(` `$n` `)); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

Output :

10

** Time Complexity : O(N) **

**Method 2 :- **The idea is the sum of first n even number is n(n+1), for find the Average of first n even numbers divide by n, hence formula is **n(n + 1) / n = ( n + 1)**. i.e. Average of first n even numbers is **n+1**. it take 0(1) time.

Avg of sum of N even natural number = (N + 1)

**Proof**

Sum of first n terms of an A.P.(Arithmetic Progression) = (n/2) * [2*a + (n-1)*d].....(i) where, a is the first term of the series and d is the difference between the adjacent terms of the series. Here, a = 2, d = 2, applying these values to eq.(i), get Sum = (n/2) * [2*2 + (n-1)*2] = (n/2) * [4 + 2*n - 2] = (n/2) * (2*n + 2) = n * (n + 1) finding the Avg so divided by n = n*(n+1)/n = (n+1)

## C++

`// CPP Program to find the average ` `// of sum of first n even numbers ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Return the average of sum ` `// of first n even numbers ` `int` `avg_of_even_num(` `int` `n) ` `{ ` ` ` `return` `n+1; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 8; ` ` ` `cout << avg_of_even_num(n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find the average ` `// of sum of first n even numbers ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Return the average of sum ` ` ` `// of first n even numbers ` ` ` `static` `int` `avg_of_even_num(` `int` `n) ` ` ` `{ ` ` ` `return` `n + ` `1` `; ` ` ` `} ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` ` ` `int` `n = ` `8` `; ` ` ` `System.out.println(avg_of_even_num(n)); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 Program to ` `# find the average ` `# of sum of first n ` `# even numbers ` ` ` `# Return the average of sum ` `# of first n even numbers ` `def` `avg_of_even_num(n) : ` ` ` ` ` `return` `n` `+` `1` ` ` ` ` `# Driven Program ` `n ` `=` `8` `print` `(avg_of_even_num(n)) ` ` ` ` ` `# This code is contributed ` `# by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to find the average ` `// of sum of first n even numbers ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Return the average of sum ` ` ` `// of first n even numbers ` ` ` `static` `int` `avg_of_even_num(` `int` `n) ` ` ` `{ ` ` ` `return` `n + 1; ` ` ` `} ` ` ` ` ` `// driver code ` ` ` `public` `static` `void` `Main () { ` ` ` ` ` `int` `n = 8; ` ` ` `Console.Write(avg_of_even_num(n)); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to find the average ` `// of sum of first n even numbers ` ` ` `// Return the average of sum ` `// of first n even numbers ` `function` `avg_of_even_num(` `$n` `) ` `{ ` ` ` `return` `$n` `+ 1; ` `} ` ` ` `// Driver Code ` `$n` `= 8; ` `echo` `(avg_of_even_num(` `$n` `)); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

Output:

9

** Time Complexity : O(1) **

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Average of Cubes of first N natural numbers
- Find the average of first N natural numbers
- Average of Squares of Natural Numbers
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Average of first n odd naturals numbers
- Average of a stream of numbers
- Average of odd numbers till a given odd number
- Average of even numbers till a given even number
- Find average of two numbers using bit operation
- Compute average of two numbers without overflow
- Natural Numbers
- Sum of first n natural numbers
- LCM of First n Natural Numbers
- Sum of squares of first n natural numbers
- Sum of kth powers of first n natural numbers
- Repeated sum of first N natural numbers
- Sum of squares of first n natural numbers
- Sum of first N natural numbers which are divisible by X or Y
- Sum of fifth powers of the first n natural numbers
- Sum of cubes of even and odd natural numbers

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.