# C++ program to find Sum of cubes of first N Even numbers

Here, we are going to learn how to **find the sum of the cubes of first N even numbers in C++ programming language**?

Submitted by Vivek Kothari, on November 09, 2018

The problem is we have a number **N** and we have to find sum of first **N** Even natural numbers.

**Example:**

Input: n = 3 Output: 288 (2^3 + 4^3+6^3)

A **simple solution** is given below...

**Example 1:**

#include <iostream> using namespace std; int calculate(int n) { int sum = 0; for (int i = 1; i <= n; i++) sum = sum + (2*i) * (2*i) * (2*i); return sum; } int main() { int num = 3; cout<<"Number is = "<<num<<endl; cout << "Sum of cubes of first "<<num<<" even number is ="<<calculate(num); return 0; }

**Output**

Number is = 3 Sum of cubes of first 3 even number is =288

The **efficient approach** is discussed below:

The sum of cubes of first n natural numbers is given by =(n*(n+1) / 2)^2Sum of cubes of first n natural numbers can be written as... =2^3 + 4^3 + .... + (2n)^3Now take out common term i.e2^3=2^3 * (1^3 + 2^3 + .... + n^3)=2^3* (n*(n+1) / 2)^2=8 * ((n^2)(n+1)^2)/4=2 * n^2(n+1)^2

Now we can apply this formula directly to **find the sum of cubes of first n even numbers**.

**Example 2:**

#include <iostream> using namespace std; int calculate(int n) { int sum = 2 * n * n * (n + 1) * (n + 1); return sum; } int main() { int num = 3; cout<<"Number is = "<<num<<endl; cout << "Sum of cubes of first "<<num<<" even number is ="<<calculate(num); return 0; }

**Output**

Number is = 3 Sum of cubes of first 3 even number is =288

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.