Sum of first n even numbers
Last Updated :
17 Jul, 2022
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++
// C++ implementation to find sum of
// first n even numbers
#include <bits/stdc++.h>
using namespace std;
// function to find sum of
// first n even numbers
int evenSum(int n)
{
int curr = 2, sum = 0;
// sum of first n even numbers
for (int i = 1; i <= n; i++) {
sum += curr;
// next even number
curr += 2;
}
// required sum
return sum;
}
// Driver program to test above
int main()
{
int n = 20;
cout << "Sum of first " << n
<< " Even numbers is: " << evenSum(n);
return 0;
}
Java
// Java implementation to find sum
// of first n even numbers
import java.util.*;
import java.lang.*;
public class GfG{
// function to find sum of
// first n even numbers
static int evenSum(int n)
{
int curr = 2, sum = 0;
// sum of first n even numbers
for (int i = 1; i <= n; i++) {
sum += curr;
// next even number
curr += 2;
}
// required sum
return sum;
}
// driver function
public static void main(String argc[])
{
int n = 20;
System.out.println("Sum of first " + n +
" Even numbers is: " +
evenSum(n));
}
}
// This code is contributed by Prerna Saini
Python3
# Python3 implementation to find sum of
# first n even numbers
# function to find sum of
# first n even numbers
def evensum(n):
curr = 2
sum = 0
i = 1
# sum of first n even numbers
while i <= n:
sum += curr
# next even number
curr += 2
i = i + 1
return sum
# Driver Code
n = 20
print("sum of first ", n, "even number is: ",
evensum(n))
# This article is contributed by rishabh_jain
C#
// C# implementation to find sum
// of first n even numbers
using System;
public class GfG {
// function to find sum of
// first n even numbers
static int evenSum(int n)
{
int curr = 2, sum = 0;
// sum of first n even numbers
for (int i = 1; i <= n; i++) {
sum += curr;
// next even number
curr += 2;
}
// required sum
return sum;
}
// driver function
public static void Main()
{
int n = 20;
Console.WriteLine("Sum of first " + n
+ " Even numbers is: " + evenSum(n));
}
}
// This code is contributed by vt-m.
PHP
<?php
// PHP implementation to find sum of
// first n even numbers
// function to find sum of
// first n even numbers
function evenSum($n)
{
$curr = 2;
$sum = 0;
// sum of first n even numbers
for ($i = 1; $i <= $n; $i++) {
$sum += $curr;
// next even number
$curr += 2;
}
// required sum
return $sum;
}
// Driver program to test above
$n = 20;
echo "Sum of first ".$n." Even numbers is: ".evenSum($n);
// this code is contributed by mits
?>
JavaScript
<script>
// JavaScript implementation to find sum of
// first n even numbers
// function to find sum of
// first n even numbers
function evenSum(n)
{
let curr = 2, sum = 0;
// sum of first n even numbers
for (let i = 1; i <= n; i++) {
sum += curr;
// next even number
curr += 2;
}
// required sum
return sum;
}
// Driver program to test above
let n = 20;
document.write("Sum of first " + n +
" Even numbers is: " + evenSum(n));
//This code is contributed by Surbhi Tyagi
</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++
// C++ implementation to find sum of
// first n even numbers
#include <bits/stdc++.h>
using namespace std;
// function to find sum of
// first n even numbers
int evenSum(int n)
{
// required sum
return (n * (n + 1));
}
// Driver program to test above
int main()
{
int n = 20;
cout << "Sum of first " << n
<< " Even numbers is: " << evenSum(n);
return 0;
}
Java
// Java implementation to find sum
// of first n even numbers
import java.util.*;
import java.lang.*;
public class GfG{
// function to find sum of
// first n even numbers
static int evenSum(int n)
{
// required sum
return (n * (n + 1));
}
// driver function
public static void main(String argc[])
{
int n = 20;
System.out.println("Sum of first " + n +
" Even numbers is: " +
evenSum(n));
}
}
// This code is contributed by Prerna Saini
Python3
# Python3 implementation to find
# sum of first n even numbers
# function to find sum of
# first n even numbers
def evensum(n):
return n * (n + 1)
# Driver Code
n = 20
print("sum of first", n, "even number is: ",
evensum(n))
# This article is contributed by rishabh_jain
C#
// C# implementation to find sum
// of first n even numbers'
using System;
public class GfG {
// function to find sum of
// first n even numbers
static int evenSum(int n)
{
// required sum
return (n * (n + 1));
}
// driver function
public static void Main()
{
int n = 20;
Console.WriteLine("Sum of first " + n
+ " Even numbers is: " + evenSum(n));
}
}
// This code is contributed by vt_m
PHP
<?php
// PHP implementation
// to find sum of
// first n even numbers
// function to find sum of
// first n even numbers
function evenSum($n)
{
// required sum
return ($n * ($n + 1));
}
// Driver Code
$n = 20;
echo "Sum of first " , $n,
" Even numbers is: " ,
evenSum($n);
// This code is contributed
// by akt_mit
?>
JavaScript
<script>
// Javascript implementation
// to find sum of
// first n even numbers
// function to find sum of
// first n even numbers
function evenSum(n)
{
// required sum
return (n * (n + 1));
}
// Driver Code
let n = 20;
document.write("Sum of first " + n +
" Even numbers is: " ,
evenSum(n));
// This code is contributed
// by gfgking
</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++
// C++ implementation to find sum of
// first n even numbers
#include <bits/stdc++.h>
using namespace std;
// function to find sum of
// first n even numbers
int evenSum(int n)
{
int tn = 2+(n-1)*2;
//find Nth Term
//calculate a+(n-1)d
//first term is = 2
//common difference is 2
//first term and common difference is same all time
// required sum
return (n/2) * (2 + tn);
//calculate (N/2) * (a + Tn)
}
// Driver program to test above
int main()
{
int n = 20;
cout << "Sum of first " << n
<< " Even numbers is: " << evenSum(n);
return 0;
}
//Contributed by SoumikMondal
Java
// java implementation to find sum of
// first n even numbers
import java.io.*;
import java.util.*;
class GFG
{
// function to find sum of
// first n even numbers
public static int evenSum(int n)
{
int tn = 2+(n-1)*2;
//find Nth Term
//calculate a+(n-1)d
//first term is = 2
//common difference is 2
//first term and common difference is same all time
// required sum
return (n/2) * (2 + tn);
//calculate (N/2) * (a + Tn)
}
// Driver program to test above
public static void main(String[] args)
{
int n = 20;
System.out.println("Sum of first "+n+" Even numbers is: "+evenSum(n));
}
}
//this code is contributed by aditya942003patil
Python3
# python3 implementation to find sum of
# first n even numbers
# function to find sum of
# first n even numbers
def evenSum(n) :
tn = 2+(n-1)*2;
#find Nth Term
#calculate a+(n-1)d
#first term is = 2
#common difference is 2
#first term and common difference is same all time
# required sum
return (int)(n/2) * (2 + tn);
#calculate (N/2) * (a + Tn)
# Driver code
if __name__ == "__main__" :
n = 20;
print("Sum of first", n ,"Even numbers is:", evenSum(n));
# this code is contributed by aditya942003patil
C#
// c# implementation to find sum of
// first n even numbers
using System;
public class GFG {
// function to find sum of
// first n even numbers
static int evenSum(int n)
{
int tn = 2+(n-1)*2;
//find Nth Term
//calculate a+(n-1)d
//first term is = 2
//common difference is 2
//first term and common difference is same all time
// required sum
return (n/2) * (2 + tn);
//calculate (N/2) * (a + Tn)
}
// Driver program to test above
public static void Main()
{
int n = 20;
// Function call
Console.Write("Sum of first "+n+" Even numbers is: "+evenSum(n));
}
}
// This code is contributed by aditya942003patil
JavaScript
<script>
// javascript implementation to find sum of
// first n even numbers
// function to find sum of
// first n even numbers
function evenSum(n)
{
var tn = 2+(n-1)*2;
//find Nth Term
//calculate a+(n-1)d
//first term is = 2
//common difference is 2
//first term and common difference is same all time
// required sum
return (n/2) * (2 + tn);
//calculate (N/2) * (a + Tn)
// Array to store frequency of each character
}
// Driver program to test above
var n = 20;
// Function call
document.write("Sum of first "+n+" Even numbers is: "+evenSum(n));
// This code is contributed by aditya942003patil.
</script>
OutputSum of first 20 Even numbers is: 420
Time Complexity: O(1).
Auxiliary Space: O(1) since using constant variables
Similar Reads
Sum of cubes of first n even numbers
Given a number n, find the sum of first n even natural numbers. Examples: Input : 2 Output : 72 2^3 + 4^3 = 72Input : 8 Output :10368 2^3 + 4^3 + 6^3 + 8^3 + 10^3 + 12^3 + 14^3 + 16^3 = 10368 A simple solution is to traverse through n even numbers and find the sum of cubes. C++ // Simple C++ method
5 min read
Sum of square of first n even numbers
Given a number n, find sum of square of first n even natural numbers. Examples: Input : 3Output : 56 22 + 42 + 62 = 56 Input : 8Output : 81622 + 42 + 62 + 82 + 102 + 122 + 142 + 162 = 816 Recommended PracticePower of Pow | Even NumberTry It! A simple solution is to traverse through n even numbers an
5 min read
Find sum of even factors of a number
Given a number n, the task is to find the even factor sum of a number.Examples: Input : 30 Output : 48 Even dividers sum 2 + 6 + 10 + 30 = 48 Input : 18 Output : 26 Even dividers sum 2 + 6 + 18 = 26 Recommended PracticeFind sum of even factors of a numberTry It! Prerequisite : Sum of factorsAs discu
8 min read
Even Fibonacci Numbers Sum
Given a limit, find the sum of all the even-valued terms in the Fibonacci sequence below given limit.The first few terms of Fibonacci Numbers are, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233 ,... (Even numbers are highlighted).Examples : Input : limit = 8 Output : 10 Explanation : 2 + 8 = 10 Inpu
6 min read
Sum of first K even-length Palindrome numbers
Given a integer k, find the sum of first k even-length palindrome numbers. Even length here refers to the number of digits of a number is even. Examples: Input : k = 3 Output : 66 Explanation: 11 + 22 + 33 = 66 (Sum of first three even-length palindrome numbers) Input : 10 Output : 1496 Explanation:
9 min read
Sum of the first N Pronic Numbers
Given a number N, the task is to find the sum of the first N Pronic Numbers. The numbers that can be arranged to form a rectangle are called Rectangular Numbers (also known as Pronic numbers). The first few rectangular numbers are: 0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 2
3 min read
Sum of Sums first n natural numbers
Given a positive integer n. The task is to find the sum of the sum of first n natural number.Examples: Input: n = 3Output: 10Explanation: Sum of first natural number: 1Sum of first and second natural number: 1 + 2 = 3Sum of first, second and third natural number = 1 + 2 + 3 = 6Sum of sum of first th
6 min read
Average of first n even natural numbers
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 s
6 min read
Represent N as sum of K even numbers
Given two integers N and K, the task is to represent N as the sum of K even number. If it is not possible to represent the number, print -1.Note: The representation may contain duplicate even numbers. Examples: Input: N = 6, K = 3 Output: 2 2 2 Explanation: The given number 6 can be represented as 2
4 min read
Sum of square of first n odd numbers
Given a number n, find sum of square of first n odd natural numbers. Examples : Input : 3 Output : 35 12 + 32 + 52 = 35 Input : 8 Output : 680 12 + 32 + 52 + 72 + 92 + 112 + 132 + 152 Recommended PracticePower of Pow | Odd NumbersTry It! A simple solution is to traverse through n odd numbers and fin
5 min read