With a given integral number n, write a program to calculate the sum of cubes.
Cubes mean power of 3. Hence, the cube of 9 is 9**3.
Here you have to calculate the cube of a series. If you want to calculate the cube up to the number n. The series will look like-
1^3+2^3+3^3+4^3+ .. .. .. +n^3
If you remembered the sum of the square, this one will be easier for you.
def cube_sum(num):
sum = 0
for n in range(num+1):
sum = sum + n**3
return sum
user_num = int(input('Enter a number: '))
result = cube_sum(user_num)
print('Your sum of cubes are: ', result)
There is an alternative solution to calculate the sum of cube of the n numbers. You can use
(n*(n+1)/2)^2
n = int(input('Enter a number: '))
sum = (n*(n+1)/2)**2
print('Your sum of cubes are: ', sum)
Sum of a series might have an easier formula.
tags: programming-hero
python
python3
problem-solving
programming
coding-challenge
interview
learn-python
python-tutorial
programming-exercises
programming-challenges
programming-fundamentals
programming-contest
python-coding-challenges
python-problem-solving