Given a number n. The problem is to find the sum of first n even numbers.
Examples:
Input : n = 4
Output : 20
Sum of first 4 even numbers
= (2 + 4 + 6 + 8) = 20
Input : n = 20
Output : 420
Naive Approach: Iterate through the first n even numbers and add them.
C++
#include <bits/stdc++.h>
using namespace std;
int evenSum( int n)
{
int curr = 2, sum = 0;
for ( int i = 1; i <= n; i++) {
sum += curr;
curr += 2;
}
return sum;
}
int main()
{
int n = 20;
cout << "Sum of first " << n
<< " Even numbers is: " << evenSum(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG{
static int evenSum( int n)
{
int curr = 2 , sum = 0 ;
for ( int i = 1 ; i <= n; i++) {
sum += curr;
curr += 2 ;
}
return sum;
}
public static void main(String argc[])
{
int n = 20 ;
System.out.println( "Sum of first " + n +
" Even numbers is: " +
evenSum(n));
}
}
|
Python3
def evensum(n):
curr = 2
sum = 0
i = 1
while i < = n:
sum + = curr
curr + = 2
i = i + 1
return sum
n = 20
print ( "sum of first " , n, "even number is: " ,
evensum(n))
|
C#
using System;
public class GfG {
static int evenSum( int n)
{
int curr = 2, sum = 0;
for ( int i = 1; i <= n; i++) {
sum += curr;
curr += 2;
}
return sum;
}
public static void Main()
{
int n = 20;
Console.WriteLine( "Sum of first " + n
+ " Even numbers is: " + evenSum(n));
}
}
|
PHP
<?php
function evenSum( $n )
{
$curr = 2;
$sum = 0;
for ( $i = 1; $i <= $n ; $i ++) {
$sum += $curr ;
$curr += 2;
}
return $sum ;
}
$n = 20;
echo "Sum of first " . $n . " Even numbers is: " .evenSum( $n );
?>
|
Javascript
<script>
function evenSum(n)
{
let curr = 2, sum = 0;
for (let i = 1; i <= n; i++) {
sum += curr;
curr += 2;
}
return sum;
}
let n = 20;
document.write( "Sum of first " + n +
" Even numbers is: " + evenSum(n));
</script>
|
OutputSum of first 20 Even numbers is: 420
Time Complexity: O(n)
Auxiliary Space: O(1)
Efficient Approach: By applying the formula given below.
Sum of first n even numbers = n * (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), we get
Sum = (n/2) * [2*2 + (n-1)*2]
= (n/2) * [4 + 2*n - 2]
= (n/2) * (2*n + 2)
= n * (n + 1)
C++
#include <bits/stdc++.h>
using namespace std;
int evenSum( int n)
{
return (n * (n + 1));
}
int main()
{
int n = 20;
cout << "Sum of first " << n
<< " Even numbers is: " << evenSum(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG{
static int evenSum( int n)
{
return (n * (n + 1 ));
}
public static void main(String argc[])
{
int n = 20 ;
System.out.println( "Sum of first " + n +
" Even numbers is: " +
evenSum(n));
}
}
|
Python3
def evensum(n):
return n * (n + 1 )
n = 20
print ( "sum of first" , n, "even number is: " ,
evensum(n))
|
C#
using System;
public class GfG {
static int evenSum( int n)
{
return (n * (n + 1));
}
public static void Main()
{
int n = 20;
Console.WriteLine( "Sum of first " + n
+ " Even numbers is: " + evenSum(n));
}
}
|
PHP
<?php
function evenSum( $n )
{
return ( $n * ( $n + 1));
}
$n = 20;
echo "Sum of first " , $n ,
" Even numbers is: " ,
evenSum( $n );
?>
|
Javascript
<script>
function evenSum(n)
{
return (n * (n + 1));
}
let n = 20;
document.write( "Sum of first " + n +
" Even numbers is: " ,
evenSum(n));
</script>
|
OutputSum of first 20 Even numbers is: 420
Time Complexity: O(1).
Space Complexity: O(1) since using constant variables
Another method:
In this method, we have to calculate the Nth term,
The formula for finding Nth term ,Tn = a+(n-1)d, here, a= first term, d= common difference, n= number of term
And then we have to apply the formula for finding the sum,
the formula is, Sn=(N/2) * (a + Tn), here a= first term, Tn= last term, n= number of term
This formula also can be applied for the sum of odd numbers, but the series must have a same common difference.
C++
#include <bits/stdc++.h>
using namespace std;
int evenSum( int n)
{
int tn = 2+(n-1)*2;
return (n/2) * (2 + tn);
}
int main()
{
int n = 20;
cout << "Sum of first " << n
<< " Even numbers is: " << evenSum(n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static int evenSum( int n)
{
int tn = 2 +(n- 1 )* 2 ;
return (n/ 2 ) * ( 2 + tn);
}
public static void main(String[] args)
{
int n = 20 ;
System.out.println( "Sum of first " +n+ " Even numbers is: " +evenSum(n));
}
}
|
Python3
def evenSum(n) :
tn = 2 + (n - 1 ) * 2 ;
return ( int )(n / 2 ) * ( 2 + tn);
if __name__ = = "__main__" :
n = 20 ;
print ( "Sum of first" , n , "Even numbers is:" , evenSum(n));
|
C#
using System;
public class GFG {
static int evenSum( int n)
{
int tn = 2+(n-1)*2;
return (n/2) * (2 + tn);
}
public static void Main()
{
int n = 20;
Console.Write( "Sum of first " +n+ " Even numbers is: " +evenSum(n));
}
}
|
Javascript
<script>
function evenSum(n)
{
var tn = 2+(n-1)*2;
return (n/2) * (2 + tn);
}
var n = 20;
document.write( "Sum of first " +n+ " Even numbers is: " +evenSum(n));
</script>
|
OutputSum of first 20 Even numbers is: 420
Time Complexity: O(1).
Auxiliary Space: O(1) since using constant variables