Print the sum of series 1^{3} + 2^{3} + 3^{3} + 4^{3} + …….+ n^{3} till n-th term.

Examples:

Input : n = 5 Output : 225 1^{3}+ 2^{3}+ 3^{3}+ 4^{3}+ 5^{3}= 225 Input : n = 7 Output : 784 1^{3}+ 2^{3}+ 3^{3}+ 4^{3}+ 5^{3}+ 6^{3}+ 7^{3}= 784

## Java

`// Simple Java program to find sum of series` `// with cubes of first n natural numbers` ` ` `import` `java.util.*;` `import` `java.lang.*;` `class` `GFG` `{` ` ` ` ` `/* Returns the sum of series */` ` ` `public` `static` `int` `sumOfSeries(` `int` `n)` ` ` `{` ` ` `int` `sum = ` `0` `;` ` ` `for` `(` `int` `x=` `1` `; x<=n; x++)` ` ` `sum += x*x*x;` ` ` `return` `sum;` ` ` `}` ` ` `// Driver Function` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `n = ` `5` `;` ` ` `System.out.println(sumOfSeries(n));` ` ` ` ` `}` `}` ` ` `// Code Contributed by Mohit Gupta_OMG <(0_o)>` |

**Output :**

225

Time Complexity : O(n)

An **efficient solution** is to use direct mathematical formula which is** (n ( n + 1 ) / 2) ^ 2**

For n = 5 sum by formula is (5*(5 + 1 ) / 2)) ^ 2 = (5*6/2) ^ 2 = (15) ^ 2 = 225 For n = 7, sum by formula is (7*(7 + 1 ) / 2)) ^ 2 = (7*8/2) ^ 2 = (28) ^ 2 = 784

## Java

`// A formula based Java program to find sum` `// of series with cubes of first n natural ` `// numbers` ` ` `import` `java.util.*;` `import` `java.lang.*;` `class` `GFG` `{` ` ` `/* Returns the sum of series */` ` ` `public` `static` `int` `sumOfSeries(` `int` `n)` ` ` `{` ` ` `int` `x = (n * (n + ` `1` `) / ` `2` `);` ` ` ` ` `return` `x * x;` ` ` `}` ` ` `// Driver Function` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `n = ` `5` `;` ` ` `System.out.println(sumOfSeries(n));` ` ` ` ` `}` `}` ` ` `// Code Contributed by Mohit Gupta_OMG <(0_o)>` |

**Output:**

225

Time Complexity : O(1)**How does this formula work?**

We can prove the formula using mathematical induction. We can easily see that the formula holds true for n = 1 and n = 2. Let this be true for n = k-1.

Let the formula be true for n = k-1. Sum of first (k-1) natural numbers = [((k - 1) * k)/2]^{2}Sum of first k natural numbers = = Sum of (k-1) numbers + k^{3}= [((k - 1) * k)/2]^{2}+ k^{3}= [k^{2}(k^{2}- 2k + 1) + 4k^{3}]/4 = [k^{4}+ 2k^{3}+ k^{2}]/4 = k^{2}(k^{2}+ 2k + 1)/4 = [k*(k+1)/2]^{2}

**The above program causes overflow, even if result is not beyond integer limit.** Like previous post, we can avoid overflow upto some extent by doing division first.

## Java

`// Efficient Java program to find sum of cubes ` `// of first n natural numbers that avoids ` `// overflow if result is going to be withing ` `// limits.` `import` `java.util.*;` `import` `java.lang.*;` `class` `GFG` `{` ` ` `/* Returns the sum of series */` ` ` `public` `static` `int` `sumOfSeries(` `int` `n)` ` ` `{` ` ` `int` `x;` ` ` `if` `(n % ` `2` `== ` `0` `)` ` ` `x = (n/` `2` `) * (n+` `1` `);` ` ` `else` ` ` `x = ((n + ` `1` `) / ` `2` `) * n;` ` ` `return` `x * x;` ` ` `}` ` ` `// Driver Function` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `n = ` `5` `;` ` ` `System.out.println(sumOfSeries(n));` ` ` `}` `}` `// Code Contributed by Mohit Gupta_OMG <(0_o)>` |

**Output:**

225

Please refer complete article on Program for cube sum of first n natural numbers for more details!

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.