Python Program to find Sum of Cubes of first n natural numbers

This python program accepts a positive integer n from user and calculates the sum of cubes up to n as
13 + 23 + 33 + .... + n3.

For example: If n = 5 then 13 + 23 + 33 + 43 + 53 = 55

Python program to find the sum of squares of first n natural numbers

def sumOfCubes(n) :
    sum = 0
    for i in range(1, n+1) : 
        sum = sum + (i * i * i) 
    print("Sum of cubes of first",n, "natural numbers is",sum)

n = 5
# un comment below code to accept number from user
# n = int(input("Enter any positive integer: "))
sumOfCubes(n)

Output:

Sum of cubes of first 5 natural numbers is 225

Comments