In this Python program, we will learn how to calculate the sum of the given series 1²+2²+3²+….+n².
Here is the source code of the program to calculate the sum of the given series 1²+2²+3²+….+n².
# 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))
Enter any Positive Number: 10
The Sum of Series upto 10 = 385.0
# 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))
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
# 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)
Enter any Positive Number: 10
The Sum of Series upto 10 = 385
Comments