Promedio de números pares hasta un número par dado

Dado un número par n, encuentre el promedio de los números pares del 1 al n.
Ejemplos: 

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

Método 1 
Podemos calcular el promedio sumando cada número par hasta n y luego dividiendo la suma por el conteo.
 

C++

// Program to find average of even numbers
// till a given even number.
#include <stdio.h>
  
// 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

<?php
// PHP Program to find average of even
// numbers till a given even number.
 
// Function to calculate the
// average of even numbers
function 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.
?>

Javascript

<script>
 
// javascript Program to find average of even numbers
// till a given even number.
 
  
// Function to calculate the average
// of even numbers
function 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 function
let n = 16;
    document.write( averageEven(n));
     
// This code is contributed by todaysgaurav
 
 
</script>

Producción: 

9

Complejidad de tiempo: O(n)

Espacio auxiliar: O(1)
Método 2 
El promedio de números pares se puede encontrar solo en pasos individuales 
usando la siguiente fórmula: – 
[n + 2] / 2 donde n es el último número par.
¿Cómo funciona esta fórmula?  
Sabemos que hay (n)/2 números pares hasta n. Por ejemplo, hay dos números pares hasta el 4 y hay tres números pares hasta el 6.
La suma de los primeros k números pares es k * (k + 1)
Poniendo k = n/2, obtenemos la suma de los primeros n/2 números pares como n/2(n/2 + 1)
Promedio de los primeros n/2 números pares (o números pares hasta n) = (n + 2)/2
 

C++

// 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 numbers
int averageEven(int n)
{
    if (n % 2 != 0) {
        cout<<"Invalid Input";
        return -1;
    }
  
    return (n + 2) / 2;
}
  
// driver function
int main()
{
    int n = 16;
    cout<<averageEven(n)<<endl;
    return 0;
}
 
// This code is contributed by noob2000.

C

// Program to find average of even numbers
// till a given even number.
#include <stdio.h>
  
// 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

<?php
// PHP Program to find average of even
// numbers till a given even number.
 
// Function to calculate the average
// of even numbers
function 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.
?>

Javascript

<script>
// javascript Program to find average of even numbers
// till a given even number.
  
// Function to calculate the average
// of even numbers
function 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>

Producción : 
 

9

Complejidad temporal: O(1) desde que se realizan operaciones constantes

Complejidad espacial: O(1) ya que usa variables constantes

Publicación traducida automáticamente

Artículo escrito por vt_m y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *