Given a number n, find sum of first n even natural numbers.

Examples:

Input : 2 Output : 72 2^3 + 4^3 = 72 Input : 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 to find sum of cubes of` `// first n even numbers.` `#include <iostream>` `using` `namespace` `std;` `int` `cubeSum(` `int` `n)` `{` ` ` `int` `sum = 0;` ` ` `for` `(` `int` `i = 1; i <= n; i++)` ` ` `sum += (2*i) * (2*i) * (2*i);` ` ` `return` `sum;` `}` `int` `main()` `{` ` ` `cout << cubeSum(8);` ` ` `return` `0;` `}` |

## Java

`// Java program to perform` `// sum of cubes of first` `// n even natural numbers` `public` `class` `GFG` `{` ` ` `public` `static` `int` `cubesum(` `int` `n)` ` ` `{` ` ` `int` `sum = ` `0` `;` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++)` ` ` `sum += (` `2` `* i) * (` `2` `* i)` ` ` `* (` `2` `* i);` ` ` ` ` `return` `sum;` ` ` `}` ` ` ` ` `// Driver function` ` ` `public` `static` `void` `main(String args[])` ` ` `{` ` ` `int` `a = ` `8` `;` ` ` `System.out.println(cubesum(a));` ` ` ` ` `}` `}` `// This code is contributed by Akansh Gupta` |

## Python3

`# Python3 program to find sum of` `# cubes of first n even numbers` `# Function to find sum of cubes` `# of first n even numbers` `def` `cubeSum(n):` ` ` `sum` `=` `0` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `):` ` ` `sum` `+` `=` `(` `2` `*` `i) ` `*` `(` `2` `*` `i) ` `*` `(` `2` `*` `i)` ` ` `return` `sum` `# Driven code` `print` `(cubeSum(` `8` `))` `# This code is contributed by Shariq Raza` |

## C#

`// C# program to perform` `// sum of cubes of first` `// n even natural numbers` `using` `System;` `public` `class` `GFG` `{` ` ` `public` `static` `int` `cubesum(` `int` `n)` ` ` `{` ` ` `int` `sum = 0;` ` ` `for` `(` `int` `i = 1; i <= n; i++)` ` ` `sum += (2 * i) * (2 * i)` ` ` `* (2 * i);` ` ` ` ` `return` `sum;` ` ` `}` ` ` ` ` `// Driver function` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `int` `a = 8;` ` ` `Console.WriteLine(cubesum(a));` ` ` ` ` `}` `}` `// This code is contributed by vt_m.` |

## PHP

`<?php` `// Simple PHP method to` `// find sum of cubes of` `// first n even numbers.` `function` `cubeSum(` `$n` `)` `{` ` ` `$sum` `= 0;` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$n` `; ` `$i` `++)` ` ` `$sum` `+= (2 * ` `$i` `) *` ` ` `(2 * ` `$i` `) *` ` ` `(2 * ` `$i` `);` ` ` `return` `$sum` `;` `}` `// Driver Code` `echo` `cubeSum(8);` ` ` `// This code is contributed by vt_m.` `?>` |

## Javascript

`<script>` `// JavaScript program to find sum of cubes of` `// first n even numbers.` ` ` `function` `cubeSum(n)` ` ` `{` ` ` `let sum = 0;` ` ` `for` `(let i = 1; i <= n; i++)` ` ` `sum += (2*i) * (2*i) * (2*i);` ` ` `return` `sum;` ` ` `}` ` ` ` ` `document.write(cubeSum(8)); ` `// This code is contributed by Surbhi Tyagi` `</script>` |

Output:

10368

An **efficient solution** is to apply below formula.

sum = 2 * n^{2(n+1)2 }How does it work? We know that sum of cubes of first n natural numbers is = n^{ }^{2}(n+1)^{2}/ 4 Sum of cubes of first n natural numbers = 2^3 + 4^3 + .... + (2n)^3 = 8 * (1^3 + 2^3 + .... + n^3) = 8 * n^{2}(n+1)^{2}/ 4 = 2 * n^{2}(n+1)^{2}

## C++

`// Efficient C++ method to find sum of cubes of` `// first n even numbers.` `#include <iostream>` `using` `namespace` `std;` `int` `cubeSum(` `int` `n)` `{` ` ` `return` `2 * n * n * (n + 1) * (n + 1);` `}` `int` `main()` `{` ` ` `cout << cubeSum(8);` ` ` `return` `0;` `}` |

## Java

`// Java program to perform` `// sum of cubes of first` `// n even natural numbers` `public` `class` `GFG` `{` ` ` `public` `static` `int` `cubesum(` `int` `n)` ` ` `{` ` ` ` ` `return` `2` `* n * n * (n + ` `1` `) * (n + ` `1` `);` ` ` `}` ` ` ` ` `// Driver function` ` ` `public` `static` `void` `main(String args[])` ` ` `{` ` ` `int` `a = ` `8` `;` ` ` `System.out.println(cubesum(a));` ` ` ` ` `}` `}` `// This code is contributed by Akansh Gupta` |

## Python3

`# Python3 program to find sum of` `# cubes of first n even numbers` `# Function to find sum of cubes` `# of first n even numbers` `def` `cubeSum(n):` ` ` ` ` `return` `2` `*` `n ` `*` `n ` `*` `(n ` `+` `1` `) ` `*` `(n ` `+` `1` `)` `# Driven code` `print` `(cubeSum(` `8` `))` `# This code is contributed by Shariq Raza` |

## C#

`// C# program to perform` `// sum of cubes of first` `// n even natural numbers` `using` `System;` `class` `GFG` `{` ` ` `public` `static` `int` `cubesum(` `int` `n)` ` ` `{` ` ` `return` `2 * n * n *` ` ` `(n + 1) * (n + 1);` ` ` `}` ` ` ` ` `// Driver code` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `int` `a = 8;` ` ` `Console.WriteLine(cubesum(a));` ` ` `}` `}` `// This code is contributed by vt_m.` |

## PHP

`<?php` `// Efficient PHP code to` `// find sum of cubes of` `// first n even numbers.` `function` `cubeSum(` `$n` `)` `{` ` ` `return` `2 * ` `$n` `* ` `$n` `*` ` ` `(` `$n` `+ 1) * (` `$n` `+ 1);` `}` `// Driver code` `echo` `cubeSum(8);` ` ` `// This code is contributed by vt_m.` `?>` |

## Javascript

`<script>` `// javascript program to perform` `// sum of cubes of first` `// n even natural numbers` `function` `cubesum(n)` `{ ` ` ` `return` `2 * n * n * (n + 1) * (n + 1);` `}` `// Driver function` `var` `a = 8;` `document.write(cubesum(a));` `// This code is contributed by Amit Katiyar` `</script>` |

Output:

10368

Sum of cube of first n odd natural numbers

This article is contributed by **Dharmendra kumar**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.