Program to calculate the sum of cube of n natural numbers using Python

Introduction

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

Program

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)

Output

Program to calculate the sum of cube of n natural numbers using Python

Explanation

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.

 

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.