In the previous article, we have understood the python code to find the sum of square of n natural numbers. Now we look the code to calculate the cube of n natural numbers.
Mathematical expression: (n * (n + 1) // 2) 2
def sum_cube(n): sum = 0 # Mathematical expression sum = pow((n * (n + 1) / 2), 2) print(sum) n = int(input("Please enter the number: ")) sum_cube(n)
In the above program, we have created a variable n to store the user input. The function sum_cube() calculates the sum of cube of n natural number by using mathematical expression and output is display through print() function.