Table of Contents
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
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)
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.