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

by | Feb 19, 2021 | Python Programs

Home » Python » Python Programs » Program to calculate the sum of square of n natural numbers using Python

Introduction

In this section, we will learn the python code to calculate the sum of square of n natural numbers provided by the user.

Mathematical expression: (n * (n + 1) * (2 * n + 1)) // 6

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

Program

def sum_square(n):
    sum = 0
# Mathematical expression
    sum = (n*(n+1) * (2*n + 1))//6
    print(sum)
n = int(input("Please enter the number: "))
sum_square(n)

Output

Program to calculate the sum of square of n natural numbers using Python Output

Explanation

In the above program, we have created a variable n to store the user input. The function sum_square() calculates the sum of square of n natural number by using mathematical expression and output is display through print() function.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

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

Author