Dark Mode On/Off
PUBLISHED ON: JULY 14, 2021

Python Program to find Cube Sum of First n Natural Numbers

In this tutorial, we will learn to calculate and display the cube sum of first n natural numbers. In simple words, we can say that the task is to find the sum of the cubes of the first N natural numbers in Python.

For a positive integer N, we have to find the value of,

`1³ + 2³ + 3³ + 4³.....+ N³`

To understand the solution you should know loops in Python.

The program should accept the following input, look at the sample input and output below,

Input- enter n: 13

Output- cube sum of first 13 natural numbers: 8281

1. Using loops
2. Using formula

Approach 1: Using loops

In this approach, we will be using loops for finding the cube sum of the first n numbers.

Algorithm

Step 1- Define a function to find the cube sum

Step 2- Declare a variable that will store the sum

Step 3- Define a loop that will run n times

Step 4- Inside the loop update the value of the variable which will store the cube sum

Step 5- Calculate cube of each number before adding it to the sum

Step 6- Return the value of s

Step 7- Take input of n from the user

Step 8- Pass the input as a parameter in the function

Step 9- Display the result returned by the function

Python Program 1

Look at the complete program given below to understand the implementation of the approach.

``````def CubeSum(n):
s=0
for i in range(n+1):
s+=i**3
return s
n=int(input("enter n: "))
print("sum of cubes of first {} natural numbers: ".format(n),CubeSum(n))``````

enter N: 20
Sum of cubes of first 20 natural numbers: 44100

The operator ** is used to calculate exponents, i**3 is the same as i³

The format() is a function for handling strings that permits you to do variable substitutions and data formatting. Here we have used this function to print the value of n in place of {} in the print statement.

Approach 2: Using formula

In mathematics, there is a formula to calculate the sum of cubes of first n natural numbers. We can directly put this formula to calculate our result. This will eliminate the need for a loop in our program.

``sum of squares of first n natural numbers = (n*(n+1)/2)**2``

Algorithm

Step 1-Define a function to calculate the cube sum of numbers

Step 2- Use the formula mentioned above to calculate the sum of cubes of n natural numbers

Step 3- Return the value calculated above

Step 4- Take input of n from the user

Step 5- Call the function to display the result

Python Program 2

Look at the complete program given below to understand the implementation of the approach.

``````def CubeSum(n) :
return (n*(n+1)//2)**2
n=int(input("enter N: "))
print("Sum of cubes of first {} natural numbers: ".format(n),CubeSum(n))``````

enter N: 13
Sum of cubes of first 13 natural numbers: 8281

The format() function is used in the same way as mentioned above to display the value of n in place of {} in the print statement.

Conclusion

In this tutorial, we have learned two ways by which we can calculate the sum of the squares of the first N natural numbers. One, by using a loop that will calculate the squares of N numbers and add them up to give the final result. Second, we can directly use the formula to get the value of the sum of the cubes of N natural numbers.

Want to learn coding?
Try our new interactive courses.
Over 20,000+ students enrolled.