;

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 the given series 1²+2²+3²+….+n². 

Formula 

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

Program 1: Python Program to calculate the Sum of Series 1²+2²+3²+….+n² Using Formula

Python Program to calculate the Sum of Series 1²+2²+3²+….+n² Using Formula
# Python Program to calculate the Sum of Series 1²+2²+3²+….+n² Using Formula

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

# Calculation
total = (number * (number + 1) * (2 * number + 1)) / 6

# 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  = 385.0

Program 2: Python Program to calculate the Sum of Series 1²+2²+3²+….+n² Using Formula and For Loop

Python Program to calculate the Sum of Series 1²+2²+3²+….+n² Using Formula and For Loop
# Python Program to calculate the Sum of Series 1²+2²+3²+….+n²
# Using Formula and For Loop

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

# Calculation
total = (number * (number + 1) * (2 * number + 1)) / 6

# 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^2 + " %i, end = ' ')
    else:
        print("{0}^2 = {1}".format(i, total))
Output

Enter any Positive Number: 10

The Sum of Series upto 10 = 385.0 

1^2 +  2^2 +  3^2 +  4^2 +  5^2 +  6^2 +  7^2 +  8^2 +  9^2 +  10^2 = 385.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_square_series(number):
    if(number == 0):
        return 0
    else:
        return (number * number) + sum_of_square_series(number - 1)

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

Enter any Positive Number: 10

The Sum of Series upto 10  = 385


Related Posts



Comments

Recent Posts
Tags