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

  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.


Comments

Leave a Reply

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.