;

Python Program to Calculate the Sum of Series 1³+2³+3³+….+n³


Tutorialsrack 14/05/2020 Python

In this python program, we will learn how to calculate the sum of series 1³+2³+3³+….+n³.

Formula

Here is the source code of the program to calculate the sum of series 1³+2³+3³+….+n³.

Program 1: Python Program to calculate the Sum of Series 1³+2³+3³+….+n³ Using formula and math.pow() function

Python Program to calculate the Sum of Series 1³+2³+3³+….+n³ Using formula and math.pow() function
# Python Program to calculate the Sum of Series 1³+2³+3³+….+n³
# Using formula and math.pow() function

# Import Module
import math 

# Take the Input From the User
number = int(input("Enter any Positive Number: "))
total = 0

# Calculation
total = math.pow((number * (number + 1)) /2, 2)

# Print the Output
print("The Sum of Series upto {0} = {1}".format(number, total))
Output

Enter any Positive Number: 10

The Sum of Series upto 10 = 3025.0

Program 2: Python Program to calculate the Sum of Series 1³+2³+3³+….+n³ Using formula and math.pow() Function and Display the Series

Python Program to calculate the Sum of Series 1³+2³+3³+….+n³ Using formula and math.pow() Function and Display the Series
# Python Program to calculate the Sum of Series 1³+2³+3³+….+n³
# Using formula and math.pow() Function and Display the Series

# Import Module
import math 

# Take the Input From the User
number = int(input("Enter any Positive Number: "))
total = 0

# Calculation
total = math.pow((number * (number + 1)) /2, 2)

# Print the Output
print("The Sum of Series upto {0} = {1} ".format(number,total))

# Print the Calculation of Series
for i in range(1, number + 1):
    if(i != number):
        print("%d^3 + " %i, end = ' ')
    else:
        print("{0}^3 = {1}".format(i, total))
Output

Enter any Positive Number: 10

The Sum of Series upto 10 = 3025.0 

1^3 +  2^3 +  3^3 +  4^3 +  5^3 +  6^3 +  7^3 +  8^3 +  9^3 +  10^3 = 3025.0

Program 3: Python Program to Calculate the Sum of Series 1³+2³+3³+….+n³ Using Recursion

Python Program to Calculate the Sum of Series 1³+2³+3³+….+n³ Using Recursion
# Python Program to Calculate the Sum of Series 1³+2³+3³+….+n³
# Using Recursion

# Define a Recursive Function
def sum_of_cubes_series(number):
    if(number == 0):
        return 0
    else:
        return (number * number * number) + sum_of_cubes_series(number - 1)

# Take the Input From the User
num = int(input("Enter any Positive Number: "))
total = sum_of_cubes_series(num)

print("The Sum of Series upto {0}  = {1}".format(num, total))
Output

Enter any Positive Number: 10

The Sum of Series upto 10  = 3025


Related Posts



Comments

Recent Posts
Tags