# Average of even numbers till a given even number

Given an even number n, find the average of even numbers from 1 to n.

Examples :

```Input : 10
Output : 6
Explanation:
(2 + 4 + 6 + 8 + 10 )/5
= 30/5
= 6

Input : 100
Output : 51
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1
We can calculate average by adding each even numbers till n and then dividing sum by count.

## C++

 `// Program to find average of even numbers ` `// till a given even number. ` `#include ` `  `  `// Function to calculate the average ` `// of even numbers ` `int` `averageEven(``int` `n) ` `{ ` `    ``if` `(n % 2 != 0) { ` `        ``printf``(``"Invalid Input"``); ` `        ``return` `-1; ` `    ``} ` `  `  `    ``int` `sum = 0, count = 0; ` `    ``while` `(n >= 2) { ` `  `  `        ``// count even numbers ` `        ``count++; ` `  `  `        ``// store the sum of even numbers ` `        ``sum += n; ` `  `  `        ``n = n - 2; ` `    ``} ` `    ``return` `sum / count; ` `} ` `  `  `// driver function ` `int` `main() ` `{ ` `    ``int` `n = 16; ` `    ``printf``(``"%d"``, averageEven(n)); ` `    ``return` `0; ` `} `

## Java

 `// Program to find average of even numbers ` `// till a given even number. ` `import` `java.io.*; ` `  `  `class` `GFG { ` `  `  `    ``// Function to calculate the average ` `    ``// of even numbers ` `    ``static` `int` `averageEven(``int` `n) ` `    ``{ ` `        ``if` `(n % ``2` `!= ``0``) { ` `        ``System.out.println(``"Invalid Input"``); ` `            ``return` `-``1``; ` `        ``} ` `  `  `        ``int` `sum = ``0``, count = ``0``; ` `        ``while` `(n >= ``2``) { ` `  `  `         ``// count even numbers ` `         ``count++; ` `  `  `         ``// store the sum of even numbers ` `         ``sum += n; ` `  `  `         ``n = n - ``2``; ` `        ``} ` `        ``return` `sum / count; ` `    ``} ` `  `  `    ``// driver function ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `n = ``16``; ` `        ``System.out.println(averageEven(n)); ` `    ``} ` `} ` `  `  `/*This code is contributed by Nikita Tiwari.*/`

## Python3

 `# Program to find average of ` `# even numbers till a given  ` `# even number. ` ` `  `# Function to calculate the ` `# average of even numbers ` `def` `averageEven(n) : ` ` `  `    ``if` `(n ``%` `2` `!``=` `0``) : ` `        ``print``(``"Invalid Input"``) ` `        ``return` `-``1` `     `  `    ``sm ``=` `0` `    ``count ``=` `0` ` `  `    ``while` `(n >``=` `2``) : ` ` `  `        ``# count even numbers ` `        ``count ``=` `count ``+` `1` ` `  `        ``# store the sum of even ` `        ``# numbers ` `        ``sm ``=` `sm ``+` `n ` ` `  `        ``n ``=` `n ``-` `2` `     `  `    ``return` `sm ``/``/` `count ` ` `  `# driver function ` `n ``=` `16` `print``(averageEven(n)) ` ` `  `# This code is contributed by Nikita Tiwari. `

## C#

 `// C# Program to find average  ` `// of even numbers till a  ` `// given even number. ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// Function to calculate the ` `    ``// average of even numbers ` `    ``static` `int` `averageEven(``int` `n) ` `    ``{ ` `        ``if` `(n % 2 != 0) { ` `        ``Console.Write(``"Invalid Input"``); ` `            ``return` `-1; ` `        ``} ` ` `  `        ``int` `sum = 0, count = 0; ` `        ``while` `(n >= 2) { ` ` `  `        ``// count even numbers ` `        ``count++; ` ` `  `        ``// store the sum of even numbers ` `        ``sum += n; ` ` `  `        ``n = n - 2; ` `        ``} ` `        ``return` `sum / count; ` `    ``} ` ` `  `    ``// driver function ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 16; ` `        ``Console.Write(averageEven(n)); ` `    ``} ` `} ` ` `  `/*This code is contributed by vt_m.*/`

## PHP

 `= 2)  ` `    ``{ ` ` `  `        ``// count even numbers ` `        ``\$count``++; ` ` `  `        ``// store the sum of  ` `        ``// even numbers ` `        ``\$sum` `+= ``\$n``; ` ` `  `        ``\$n` `= ``\$n` `- 2; ` `    ``} ` `    ``return` `\$sum` `/ ``\$count``; ` `} ` ` `  `    ``// Driver Code ` `    ``\$n` `= 16; ` `    ``echo``(averageEven(``\$n``)); ` `     `  `//This code is contributed by vt_m. ` `?> `

Output:

```9
```

Method 2
The average of even numbers can be find out only in single steps
by using the following formula :-
[n + 2 ] / 2 where n is last even number.

How does this formula work?
We know there are (n)/2 even numbers till n. For example there are two even number till 4 and there are three even numbers till 6.

Sum of first k even numbers is k * (k + 1)

Putting k = n/2, we get sum of first n/2 even numbers as n/2(n/2 + 1)

Average of first n/2 even numbers (or even numbers till n) = (n + 2)/2

## C

 `// Program to find average of even numbers ` `// till a given even number. ` `#include ` `  `  `// Function to calculate the average ` `// of even numbers ` `int` `averageEven(``int` `n) ` `{ ` `    ``if` `(n % 2 != 0) { ` `        ``printf``(``"Invalid Input"``); ` `        ``return` `-1; ` `    ``} ` `  `  `    ``return` `(n + 2) / 2; ` `} ` `  `  `// driver function ` `int` `main() ` `{ ` `    ``int` `n = 16; ` `    ``printf``(``"%d"``, averageEven(n)); ` `    ``return` `0; ` `} `

## Java

 `// Program to find average of even numbers ` `// till a given even number. ` `import` `java.io.*; ` ` `  `class` `GFG { ` `     `  `    ``// Function to calculate the average ` `    ``// of even numbers ` `    ``static` `int` `averageEven(``int` `n) ` `    ``{ ` `        ``if` `(n % ``2` `!= ``0``) { ` `        ``System.out.println(``"Invalid Input"``); ` `            ``return` `-``1``; ` `        ``} ` ` `  `        ``return` `(n + ``2``) / ``2``; ` `    ``} ` ` `  `    ``// driver function ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `n = ``16``; ` `        ``System.out.println(averageEven(n)); ` `    ``} ` `} ` ` `  `/*This code is contributed by Nikita Tiwari.*/`

## Python3

 `# Python3 program to find average of even  ` `# numbers till a given even number. ` ` `  `# Function to calculate the  ` `# average of even numbers ` `def` `averageEven(n) : ` `    ``if` `(n ``%` `2` `!``=` `0``) : ` `        ``print``(``"Invalid Input"``) ` `        ``return` `-``1` `     `  `    ``return` `(n ``+` `2``) ``/``/` `2` `     `  `# Driver function ` `n ``=` `16` `print``(averageEven(n)) ` ` `  `# This code is contributed by Nikita Tiwari. `

## C#

 `// C# Program to find average  ` `// of even numbers till a ` `// given even number. ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// Function to calculate the ` `    ``// average of even numbers ` `    ``static` `int` `averageEven(``int` `n) ` `    ``{ ` `        ``if` `(n % 2 != 0) { ` `        ``Console.Write(``"Invalid Input"``); ` `            ``return` `-1; ` `        ``} ` ` `  `        ``return` `(n + 2) / 2; ` `    ``} ` ` `  `    ``// driver function ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 16; ` `        ``Console.Write(averageEven(n)); ` `    ``} ` `} ` ` `  `/*This code is contributed by vt_m.*/`

## PHP

 ` `

Output :

```9
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : vt_m

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.