📜  偶数至给定偶数的平均值

📅  最后修改于: 2021-04-29 08:20:34             🧑  作者: Mango

给定偶数n,求1到n的偶数平均值。
例子 :

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

方法1
我们可以通过将每个偶数相加直到n,然后将总和除以计数来计算平均值。

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.
?>


Javascript


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


Javascript


输出:

9

方法二
偶数的平均值只能一步找到
通过使用以下公式:
[n + 2] / 2其中n是最后一个偶数。
这个公式如何运作?
我们知道直到n为止有(n)/ 2个偶数。例如,有两个偶数到4,有三个偶数到6。
前k个偶数之和为k *(k + 1)
将K = N / 2,我们得到第一n的总和/ 2的偶数为N / 2(N / 2 + 1)
前n / 2个偶数(或直到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


Java脚本


输出 :

9