Skip to content
Related Articles

Related Articles

Program for cube sum of first n natural numbers
  • Difficulty Level : Easy
  • Last Updated : 31 Mar, 2021

Print the sum of series 13 + 23 + 33 + 43 + …….+ n3 till n-th term.
Examples : 
 

Input : n = 5
Output : 225
13 + 23 + 33 + 43 + 53 = 225

Input : n = 7
Output : 784
13 + 23 + 33 + 43 + 53 + 
63 + 73 = 784

 

A simple solution is to one by one add terms. 
 

C++




// Simple C++ program to find sum of series
// with cubes of first n natural numbers
#include <iostream>
using namespace std;
 
/* Returns the sum of series */
int sumOfSeries(int n)
{
    int sum = 0;
    for (int x = 1; x <= n; x++)
        sum += x * x * x;
    return sum;
}
 
// Driver Function
int main()
{
    int n = 5;
    cout << sumOfSeries(n);
    return 0;
}

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)>

Python3




# Simple Python program to find sum of series
# with cubes of first n natural numbers
 
# Returns the sum of series
def sumOfSeries(n):
    sum = 0
    for i in range(1, n + 1):
        sum += i * i*i
         
    return sum
 
  
# Driver Function
n = 5
print(sumOfSeries(n))
 
# Code Contributed by Mohit Gupta_OMG <(0_o)>

C#




// Simple C# program to find sum of series
// with cubes of first n natural numbers
using System;
 
class GFG {
    /* Returns the sum of series */
    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()
    {
        int n = 5;
        Console.Write(sumOfSeries(n));
    }
}
// This code is contributed by
// Smitha Dinesh Semwal

PHP




<?php
// Simple PHP program to find sum of series
// with cubes of first n natural numbers
 
// Returns the sum of series
function sumOfSeries( $n)
{
    $sum = 0;
    for ($x = 1; $x <= $n; $x++)
        $sum += $x * $x * $x;
    return $sum;
}
 
// Driver code
$n = 5;
echo sumOfSeries($n);
 
// This Code is contributed by vt_m.
?>

Javascript




<script>
 
//  Simple javascript program to find sum of series
// with cubes of first n natural numbers
 
/* Returns the sum of series */
function sumOfSeries( n)
{
    let sum = 0;
    for (let x = 1; x <= n; x++)
        sum += x * x * x;
    return sum;
}
 
// Driven Program
 
    let n = 5;
     document.write(sumOfSeries(n));
 
// This code contributed by aashish1995
 
</script>

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

 

C++




// A formula based C++ program to find sum
// of series with cubes of first n natural
// numbers
#include <iostream>
using namespace std;
 
int sumOfSeries(int n)
{
    int x = (n * (n + 1) / 2);
    return x * x;
}
 
// Driver Function
int main()
{
    int n = 5;
    cout << sumOfSeries(n);
    return 0;
}

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)>

Python3




# A formula based Python program to find sum
# of series with cubes of first n natural
# numbers
 
# Returns the sum of series
def sumOfSeries(n):
    x = (n * (n + 1/ 2)
    return (int)(x * x)
 
 
  
# Driver Function
n = 5
print(sumOfSeries(n))
 
# Code Contributed by Mohit Gupta_OMG <(0_o)>

C#




// A formula based C# program to
// find sum of series with cubes
// of first n natural numbers
using System;
 
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()
    {
        int n = 5;
         
        Console.Write(sumOfSeries(n));
    }
}
 
// Code Contributed by nitin mittal.

PHP




<?php
// A formula based PHP program to find sum
// of series with cubes of first n natural
// numbers
 
function sumOfSeries($n)
{
    $x = ($n * ($n + 1) / 2);
    return $x * $x;
}
 
// Driver Function
$n = 5;
echo sumOfSeries($n);
 
// This code is contributed by vt_m.
?>

Javascript




<script>
 
// Simple javascript program to find sum of series
// with cubes of first n natural numbers
 
/* Returns the sum of series */
function sumOfSeries( n)
{
    x = (n * (n + 1) / 2)
    return (x * x)
}
 
// Driven Program
 
    let n = 5;
    document.write(sumOfSeries(n));
 
// This code is contributed by sravan kumar
 
</script>

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 + k3
          = [((k - 1) * k)/2]2 + k3
          = [k2(k2 - 2k + 1) + 4k3]/4
          = [k4 + 2k3 + k2]/4
          = k2(k2 + 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. 
 

C++




// Efficient CPP program to find sum of cubes
// of first n natural numbers that avoids
// overflow if result is going to be withing
// limits.
#include <iostream>
using namespace std;
 
// Returns sum of first n natural
// numbers
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 code
int main()
{
    int n = 5;
    cout << sumOfSeries(n);
    return 0;
}

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)>

Python3




# Efficient Python program to find sum of cubes
# of first n natural numbers that avoids
# overflow if result is going to be withing
# limits.
 
# Returns the sum of series
def sumOfSeries(n):
    x = 0
    if n % 2 == 0 :
        x = (n / 2) * (n + 1)
    else:
        x = ((n + 1) / 2) * n
         
    return (int)(x * x)
 
  
# Driver Function
n = 5
print(sumOfSeries(n))
 
# Code Contributed by Mohit Gupta_OMG <(0_o)>

C#




// Efficient C# program to find sum of
// cubes of first n natural numbers
// that avoids overflow if result is
// going to be withing limits.
using System;
 
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 code
    static public void Main ()
    {
        int n = 5;
        Console.WriteLine(sumOfSeries(n));
    }
}
 
// This code is contributed by Ajit.

PHP




<?php
// Efficient PHP program to
// find sum of cubes of first 
// n natural numbers that avoids
// overflow if result is going
// to be with in limits.
 
// Returns sum of first n
// natural numbers
function sumOfSeries($n)
{
    $x;
    if ($n % 2 == 0)
        $x = ($n / 2) * ($n + 1);
    else
        $x = (($n + 1) / 2) * $n;
    return $x * $x;
}
 
// Driver code
$n = 5;
echo sumOfSeries($n);
 
// This code is contributed by vt_m.
?>

Javascript




<script>
 
// Simple javascript program to find sum of series
// with cubes of first n natural numbers
 
/* Returns the sum of series */
function sumOfSeries( n)
{
  x=0
    if (n % 2 == 0)
        x = (n / 2) * (n + 1)
    else
        x = ((n + 1) / 2) * n
         
    return (x * x)
}
 
// Driven Program
 
    let n = 5;
    document.write(sumOfSeries(n));
 
// This code contributed by sravan
 
</script>

Output: 
 

225

This article is contributed by R_Raj. 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 mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :