# Sum of initial N Even natural numbers

The problem revolves around finding the sum of the first n even natural numbers. Even natural numbers are those positive integers that are divisible by 2. The task is to calculate the sum of the initial n even natural numbers efficiently.

## Problem Statement

Given a positive integer n, the objective is to calculate the sum of the first n even natural numbers.

## Example

Consider n = 5 as an example. Even natural numbers are: 2, 4, 6, 8, 10. The sum of these numbers is 2 + 4 + 6 + 8 + 10 = 30.

## Idea to Solve

The problem can be addressed by employing the mathematical properties of arithmetic progression. An arithmetic progression is a sequence of numbers in which the difference between consecutive terms is constant. The sum of an arithmetic progression can be calculated using the formula: sum = n * (first term + last term) / 2.

In this case, the first term is 2 (the smallest even natural number) and the last term is 2n (the nth even natural number). Thus, the sum formula becomes: sum = n * (2 + 2n) / 2 = n * (n + 1).

## Pseudocode

``````function sumOfEven(n)
result = n * (n + 1)
print "Sum of", n, "Even natural numbers is:", result``````

## Algorithm Explanation

1. Calculate the sum of the first n even natural numbers using the formula `n * (n + 1)`.
2. Print the result.

## Code Solution

``````/*
C program for
Sum of initial N Even natural numbers
*/
#include <stdio.h>

void sumOfEven(int n)
{
int result = n *(n + 1);
// Display calculated result
printf(" Sum of %d Even natural numbers is  : %d \n", n, result);
}
int main(int argc, char const *argv[])
{
/*
Test A

n = 10

(2) + (4) + (6) + (8) + (10) +
(12) + (14) + (16) + (18) + (20)
----------------------------------------
Result = 110

*/
sumOfEven(10);
/*
Test B

n = 7

(2) + (4) + (6) + (8) + (10) + (12) + (14)
----------------------------------------
Result = 56
*/
sumOfEven(7);
return 0;
}``````

#### Output

`````` Sum of 10 Even natural numbers is  : 110
Sum of 7 Even natural numbers is  : 56``````
``````/*
Java program for
Sum of initial N Even natural numbers
*/
public class NaturalNumbers
{
public void sumOfEven(int n)
{
int result = n * (n + 1);
// Display calculated result
System.out.print("\n Sum of " +
n + " Even natural numbers is : " +
result);
}
public static void main(String[] args)
{
/*
Test A

n = 10

(2) + (4) + (6) + (8) + (10) +
(12) + (14) + (16) + (18) + (20)
----------------------------------------
Result = 110

*/
/*
Test B

n = 7

(2) + (4) + (6) + (8) + (10) + (12) + (14)
----------------------------------------
Result = 56
*/
}
}``````

#### Output

`````` Sum of 10 Even natural numbers is : 110
Sum of 7 Even natural numbers is : 56``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ program for
Sum of initial N Even natural numbers
*/
class NaturalNumbers
{
public: void sumOfEven(int n)
{
int result = n *(n + 1);
// Display calculated result
cout << "\n Sum of " << n
<< " Even natural numbers is : "
<< result;
}
};
int main()
{
/*
Test A
n = 10

(2) + (4) + (6) + (8) + (10) +
(12) + (14) + (16) + (18) + (20)
----------------------------------------
Result = 110

*/
/*
Test B
n = 7

(2) + (4) + (6) + (8) + (10) + (12) + (14)
----------------------------------------
Result = 56
*/
return 0;
}``````

#### Output

`````` Sum of 10 Even natural numbers is : 110
Sum of 7 Even natural numbers is : 56``````
``````// Include namespace system
using System;
/*
Csharp program for
Sum of initial N Even natural numbers
*/
public class NaturalNumbers
{
public void sumOfEven(int n)
{
int result = n * (n + 1);
// Display calculated result
Console.Write("\n Sum of " +
n + " Even natural numbers is : " +
result);
}
public static void Main(String[] args)
{
/*
Test A
n = 10

(2) + (4) + (6) + (8) + (10) +
(12) + (14) + (16) + (18) + (20)
----------------------------------------
Result = 110

*/
/*
Test B
n = 7

(2) + (4) + (6) + (8) + (10) + (12) + (14)
----------------------------------------
Result = 56
*/
}
}``````

#### Output

`````` Sum of 10 Even natural numbers is : 110
Sum of 7 Even natural numbers is : 56``````
``````package main
import "fmt"
/*
Go program for
Sum of initial N Even natural numbers
*/

func sumOfEven(n int) {
var result int = n * (n + 1)
// Display calculated result
fmt.Print("\n Sum of ", n, " Even natural numbers is : ", result)
}
func main() {

/*
Test A
n = 10

(2) + (4) + (6) + (8) + (10) +
(12) + (14) + (16) + (18) + (20)
----------------------------------------
Result = 110

*/
sumOfEven(10)
/*
Test B
n = 7

(2) + (4) + (6) + (8) + (10) + (12) + (14)
----------------------------------------
Result = 56
*/
sumOfEven(7)
}``````

#### Output

`````` Sum of 10 Even natural numbers is : 110
Sum of 7 Even natural numbers is : 56``````
``````<?php
/*
Php program for
Sum of initial N Even natural numbers
*/
class NaturalNumbers
{
public	function sumOfEven(\$n)
{
\$result = \$n * (\$n + 1);
// Display calculated result
echo("\n Sum of ".\$n.
" Even natural numbers is : ".\$result);
}
}

function main()
{
/*
Test A
n = 10

(2) + (4) + (6) + (8) + (10) +
(12) + (14) + (16) + (18) + (20)
----------------------------------------
Result = 110

*/
/*
Test B
n = 7

(2) + (4) + (6) + (8) + (10) + (12) + (14)
----------------------------------------
Result = 56
*/
}
main();``````

#### Output

`````` Sum of 10 Even natural numbers is : 110
Sum of 7 Even natural numbers is : 56``````
``````/*
Node JS program for
Sum of initial N Even natural numbers
*/
class NaturalNumbers
{
sumOfEven(n)
{
var result = n * (n + 1);
// Display calculated result
process.stdout.write("\n Sum of " +
n + " Even natural numbers is : " +
result);
}
}

function main()
{
/*
Test A
n = 10

(2) + (4) + (6) + (8) + (10) +
(12) + (14) + (16) + (18) + (20)
----------------------------------------
Result = 110

*/
/*
Test B
n = 7

(2) + (4) + (6) + (8) + (10) + (12) + (14)
----------------------------------------
Result = 56
*/
}
main();``````

#### Output

`````` Sum of 10 Even natural numbers is : 110
Sum of 7 Even natural numbers is : 56``````
``````#    Python 3 program for
#    Sum of initial N Even natural numbers
class NaturalNumbers :
def sumOfEven(self, n) :
result = n * (n + 1)
#  Display calculated result
print("\n Sum of", n ,
"Even natural numbers is :",
result, end = "")

def main() :
#   Test A
#   n = 10
#   (2) + (4) + (6) + (8) + (10) +
#   (12) + (14) + (16) + (18) + (20)
#   ----------------------------------------
#   Result = 110
#   Test B
#   n = 7
#   (2) + (4) + (6) + (8) + (10) + (12) + (14)
#   ----------------------------------------
#   Result = 56

if __name__ == "__main__": main()``````

#### Output

`````` Sum of 10 Even natural numbers is : 110
Sum of 7 Even natural numbers is : 56``````
``````#    Ruby program for
#    Sum of initial N Even natural numbers
class NaturalNumbers
def sumOfEven(n)
result = n * (n + 1)
#  Display calculated result
print("\n Sum of ",
n ," Even natural numbers is : ",
result)
end

end

def main()
#   Test A
#   n = 10
#   (2) + (4) + (6) + (8) + (10) +
#   (12) + (14) + (16) + (18) + (20)
#   ----------------------------------------
#   Result = 110
#   Test B
#   n = 7
#   (2) + (4) + (6) + (8) + (10) + (12) + (14)
#   ----------------------------------------
#   Result = 56
end

main()``````

#### Output

`````` Sum of 10 Even natural numbers is : 110
Sum of 7 Even natural numbers is : 56``````
``````/*
Scala program for
Sum of initial N Even natural numbers
*/
class NaturalNumbers()
{
def sumOfEven(n: Int): Unit = {
var result: Int = n * (n + 1);
// Display calculated result
print("\n Sum of " +
n + " Even natural numbers is : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: NaturalNumbers = new NaturalNumbers();
/*
Test A
n = 10

(2) + (4) + (6) + (8) + (10) +
(12) + (14) + (16) + (18) + (20)
----------------------------------------
Result = 110

*/
/*
Test B
n = 7

(2) + (4) + (6) + (8) + (10) + (12) + (14)
----------------------------------------
Result = 56
*/
}
}``````

#### Output

`````` Sum of 10 Even natural numbers is : 110
Sum of 7 Even natural numbers is : 56``````
``````/*
Swift 4 program for
Sum of initial N Even natural numbers
*/
class NaturalNumbers
{
func sumOfEven(_ n: Int)
{
let result: Int = n * (n + 1);
// Display calculated result
print("\n Sum of", n ,
"Even natural numbers is :",
result, terminator: "");
}
}
func main()
{
/*
Test A
n = 10

(2) + (4) + (6) + (8) + (10) +
(12) + (14) + (16) + (18) + (20)
----------------------------------------
Result = 110

*/
/*
Test B
n = 7

(2) + (4) + (6) + (8) + (10) + (12) + (14)
----------------------------------------
Result = 56
*/
}
main();``````

#### Output

`````` Sum of 10 Even natural numbers is : 110
Sum of 7 Even natural numbers is : 56``````
``````/*
Kotlin program for
Sum of initial N Even natural numbers
*/
class NaturalNumbers
{
fun sumOfEven(n: Int): Unit
{
val result: Int = n * (n + 1);
// Display calculated result
print("\n Sum of " + n +
" Even natural numbers is : " + result);
}
}
fun main(args: Array < String > ): Unit
{
/*
Test A
n = 10

(2) + (4) + (6) + (8) + (10) +
(12) + (14) + (16) + (18) + (20)
----------------------------------------
Result = 110

*/
/*
Test B
n = 7

(2) + (4) + (6) + (8) + (10) + (12) + (14)
----------------------------------------
Result = 56
*/
}``````

#### Output

`````` Sum of 10 Even natural numbers is : 110
Sum of 7 Even natural numbers is : 56``````

## Time Complexity

The time complexity of this algorithm is constant, O(1), since it involves only a few mathematical operations that do not depend on the input size. The space complexity is also constant, as it uses a fixed number of variables.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.