Average of first n even natural numbers
Last Updated :
12 Sep, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int avg_of_even_num( int n)
{
int sum = 0;
for ( int i = 1; i <= n; i++)
sum += 2*i;
return sum/n;
}
int main()
{
int n = 9;
cout << avg_of_even_num(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int avg_of_even_num( int n)
{
int sum = 0 ;
for ( int i = 1 ; i <= n; i++)
sum += 2 *i;
return (sum / n);
}
public static void main (String[] args) {
int n = 9 ;
System.out.print(avg_of_even_num(n));
}
}
|
Python3
def avg_of_even_num(n):
sum = 0
for i in range ( 1 , n + 1 ):
sum = sum + 2 * i
return sum / n
n = 9
print (avg_of_even_num(n))
|
C#
using System;
class GFG {
static int avg_of_even_num( int n)
{
int sum = 0;
for ( int i = 1; i <= n; i++)
sum += 2 * i;
return (sum / n);
}
public static void Main () {
int n = 9;
Console.Write(avg_of_even_num(n));
}
}
|
PHP
<?php
function avg_of_even_num( $n )
{
$sum = 0;
for ( $i = 1; $i <= $n ; $i ++)
$sum += 2 * $i ;
return $sum / $n ;
}
$n = 9;
echo (avg_of_even_num( $n ));
?>
|
Javascript
<script>
function avg_of_even_num( n)
{
let sum = 0;
for (let i = 1; i <= n; i++)
sum += 2*i;
return sum/n;
}
let n = 9;
document.write(avg_of_even_num(n));
</script>
|
Output :
10
Time Complexity : O(N)
Auxiliary Space: O(1) as it is using constant space
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++
#include <bits/stdc++.h>
using namespace std;
int avg_of_even_num( int n)
{
return n+1;
}
int main()
{
int n = 8;
cout << avg_of_even_num(n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
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));
}
}
|
Python3
def avg_of_even_num(n) :
return n + 1
n = 8
print (avg_of_even_num(n))
|
C#
using System;
class GFG {
static int avg_of_even_num( int n)
{
return n + 1;
}
public static void Main () {
int n = 8;
Console.Write(avg_of_even_num(n));
}
}
|
PHP
<?php
function avg_of_even_num( $n )
{
return $n + 1;
}
$n = 8;
echo (avg_of_even_num( $n ));
?>
|
Javascript
<script>
function avg_of_even_num(n)
{
return n + 1;
}
var n = 8;
document.write(avg_of_even_num(n));
</script>
|
Output:
9
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...