Average of even numbers till a given even number - GeeksforGeeks

16 Dec, 2021
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
Method 1 We can calculate average by adding each even numbers till n and then dividing sum by count. 
C++
Java
Python3
C#
PHP
Javascript
// Program to find average of even numbers// till a given even number.#include <stdio.h>  // Function to calculate the average// of even numbersint 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 functionint main(){    int n = 16;    printf("%d", averageEven(n));    return 0;}
// 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.*/
# Program to find average of# even numbers till a given# even number. # Function to calculate the# average of even numbersdef 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 functionn = 16print(averageEven(n)) # This code is contributed by Nikita Tiwari.
// 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// PHP Program to find average of even// numbers till a given even number. // Function to calculate the// average of even numbersfunction averageEven( $n){    if ($n % 2 != 0)    {        echo("Invalid Input");        return -1;    }     $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 Code    $n = 16;    echo(averageEven($n));     //This code is contributed by vt_m.?>
<script> // javascript Program to find average of even numbers// till a given even number.   // Function to calculate the average// of even numbersfunction averageEven( n){    if (n % 2 != 0) {        document.write("Invalid Input");        return -1;    }      let 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 functionlet n = 16;    document.write( averageEven(n));     // This code is contributed by todaysgaurav  </script>
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++
C
Java
Python3
C#
PHP
Javascript
// Program to find average of even numbers// till a given even nend number.#include <bits/stdc++.h>using namespace std;  // Function to calculate the average// of even numbersint averageEven(int n){    if (n % 2 != 0) {        cout<<"Invalid Input";        return -1;    }      return (n + 2) / 2;}  // driver functionint main(){    int n = 16;    cout<<averageEven(n)<<endl;    return 0;} // This code is contributed by noob2000.
// Program to find average of even numbers// till a given even number.#include <stdio.h>  // Function to calculate the average// of even numbersint averageEven(int n){    if (n % 2 != 0) {        printf("Invalid Input");        return -1;    }      return (n + 2) / 2;}  // driver functionint main(){    int n = 16;    printf("%d", averageEven(n));    return 0;}
// 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 program to find average of even# numbers till a given even number. # Function to calculate the# average of even numbersdef averageEven(n) :    if (n % 2 != 0) :        print("Invalid Input")        return -1         return (n + 2) // 2     # Driver functionn = 16print(averageEven(n)) # This code is contributed by Nikita Tiwari.
// 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// PHP Program to find average of even// numbers till a given even number. // Function to calculate the average// of even numbersfunction averageEven( $n){    if ($n % 2 != 0)    {        echo("Invalid Input");        return -1;    }     return ($n + 2) / 2;}     // Driver Code    $n = 16;    echo (averageEven($n));    return 0; // This code is contributed by vt_m.?>
<script>// javascript Program to find average of even numbers// till a given even number.  // Function to calculate the average// of even numbersfunction averageEven( n){    if (n % 2 != 0)    {        document.write("Invalid Input");        return -1;    }    return (n + 2) / 2;}  // driver function    let n = 16;     document.write(averageEven(n));      // This code is contributed by Rajput-Ji </script>
Output :  
9
 
vt_m
todaysgaurav
Rajput-Ji
noob2000
sagar0719kumar
series
series-sum
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Modular multiplicative inverse
Count all possible paths from top left to bottom right of a mXn matrix
Fizz Buzz Implementation
Check if a number is Palindrome
Program to multiply two matrices
Merge two sorted arrays with O(1) extra space
Generate all permutation of a set in Python
Count ways to reach the n'th stair