Skip to main content

Sum of initial N Even natural numbers

The sum of the first n even natural numbers can be calculated by using the formula:

n(n+1)

To understand this formula, we first need to know that the even natural numbers are the multiples of 2, and the first few even natural numbers are:

2, 4, 6, 8, 10, 12, ...

If we add up the first n even natural numbers, we get:

2 + 4 + 6 + ... + 2n

We can factor out 2 from each term:

2(1 + 2 + 3 + ... + n)

The sum of the first n positive integers is given by the formula:

1 + 2 + 3 + ... + n = n(n+1)/2

So, substituting this into the previous equation, we get:

2(1 + 2 + 3 + ... + n) = 2n(n+1)/2 = n(n+1)

Therefore, the sum of the first n even natural numbers is n(n+1).

Here given code implementation process.

/*
    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)
	{
		NaturalNumbers task = new NaturalNumbers();
		/*
		   Test A

		   n = 10
		  
		   (2) + (4) + (6) + (8) + (10) + 
		   (12) + (14) + (16) + (18) + (20)
		   ----------------------------------------
		   Result = 110
		   
		*/
		task.sumOfEven(10);
		/*
		   Test B

		   n = 7
		   
		   (2) + (4) + (6) + (8) + (10) + (12) + (14) 
		   ----------------------------------------
		   Result = 56
		*/
		task.sumOfEven(7);
	}
}

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()
{
	NaturalNumbers *task = new NaturalNumbers();
	/*
	   Test A
	   n = 10
	  
	   (2) + (4) + (6) + (8) + (10) + 
	   (12) + (14) + (16) + (18) + (20)
	   ----------------------------------------
	   Result = 110
	   
	*/
	task->sumOfEven(10);
	/*
	   Test B
	   n = 7
	   
	   (2) + (4) + (6) + (8) + (10) + (12) + (14) 
	   ----------------------------------------
	   Result = 56
	*/
	task->sumOfEven(7);
	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)
	{
		NaturalNumbers task = new NaturalNumbers();
		/*
		   Test A
		   n = 10
		  
		   (2) + (4) + (6) + (8) + (10) + 
		   (12) + (14) + (16) + (18) + (20)
		   ----------------------------------------
		   Result = 110
		   
		*/
		task.sumOfEven(10);
		/*
		   Test B
		   n = 7
		   
		   (2) + (4) + (6) + (8) + (10) + (12) + (14) 
		   ----------------------------------------
		   Result = 56
		*/
		task.sumOfEven(7);
	}
}

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()
{
	$task = new NaturalNumbers();
	/*
	   Test A
	   n = 10
	  
	   (2) + (4) + (6) + (8) + (10) + 
	   (12) + (14) + (16) + (18) + (20)
	   ----------------------------------------
	   Result = 110
	   
	*/
	$task->sumOfEven(10);
	/*
	   Test B
	   n = 7
	   
	   (2) + (4) + (6) + (8) + (10) + (12) + (14) 
	   ----------------------------------------
	   Result = 56
	*/
	$task->sumOfEven(7);
}
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()
{
	var task = new NaturalNumbers();
	/*
	   Test A
	   n = 10
	  
	   (2) + (4) + (6) + (8) + (10) + 
	   (12) + (14) + (16) + (18) + (20)
	   ----------------------------------------
	   Result = 110
	   
	*/
	task.sumOfEven(10);
	/*
	   Test B
	   n = 7
	   
	   (2) + (4) + (6) + (8) + (10) + (12) + (14) 
	   ----------------------------------------
	   Result = 56
	*/
	task.sumOfEven(7);
}
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() :
	task = NaturalNumbers()
	#   Test A
	#   n = 10
	#   (2) + (4) + (6) + (8) + (10) + 
	#   (12) + (14) + (16) + (18) + (20)
	#   ----------------------------------------
	#   Result = 110
	task.sumOfEven(10)
	#   Test B
	#   n = 7
	#   (2) + (4) + (6) + (8) + (10) + (12) + (14) 
	#   ----------------------------------------
	#   Result = 56
	task.sumOfEven(7)

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() 
	task = NaturalNumbers.new()
	#   Test A
	#   n = 10
	#   (2) + (4) + (6) + (8) + (10) + 
	#   (12) + (14) + (16) + (18) + (20)
	#   ----------------------------------------
	#   Result = 110
	task.sumOfEven(10)
	#   Test B
	#   n = 7
	#   (2) + (4) + (6) + (8) + (10) + (12) + (14) 
	#   ----------------------------------------
	#   Result = 56
	task.sumOfEven(7)
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
		   
		*/
		task.sumOfEven(10);
		/*
		   Test B
		   n = 7
		   
		   (2) + (4) + (6) + (8) + (10) + (12) + (14) 
		   ----------------------------------------
		   Result = 56
		*/
		task.sumOfEven(7);
	}
}

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()
{
	let task: NaturalNumbers = NaturalNumbers();
	/*
	   Test A
	   n = 10
	  
	   (2) + (4) + (6) + (8) + (10) + 
	   (12) + (14) + (16) + (18) + (20)
	   ----------------------------------------
	   Result = 110
	   
	*/
	task.sumOfEven(10);
	/*
	   Test B
	   n = 7
	   
	   (2) + (4) + (6) + (8) + (10) + (12) + (14) 
	   ----------------------------------------
	   Result = 56
	*/
	task.sumOfEven(7);
}
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
{
	val task: NaturalNumbers = NaturalNumbers();
	/*
	   Test A
	   n = 10
	  
	   (2) + (4) + (6) + (8) + (10) + 
	   (12) + (14) + (16) + (18) + (20)
	   ----------------------------------------
	   Result = 110
	   
	*/
	task.sumOfEven(10);
	/*
	   Test B
	   n = 7
	   
	   (2) + (4) + (6) + (8) + (10) + (12) + (14) 
	   ----------------------------------------
	   Result = 56
	*/
	task.sumOfEven(7);
}

Output

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




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.

New Comment