Hello everyone, welcome to nkcoderz.com. In this article we will going to discuss about Python program to find squares of the summation of n numbers.
Here’s an example of a Python program that calculates the square of the summation of a given number of numbers:
Code for python program to find squares of the summation of n numbers
def summation_square(n): sum = 0 for i in range(1, n+1): num = int(input("Enter a number: ")) sum += num return sum**2 n = int(input("Enter the number of elements: ")) print("Square of the summation of", n, "numbers is", summation_square(n))
Enter the number of elements: 2
Enter a number: 1
Enter a number: 2
Square of the summation of 2 numbers is 9
This program is similar to the previous one where I explained about the summation, The only difference is, this program calculates the square of summation by using the ** operator to raise the summation result to the power of 2.
The program starts by asking the user to enter the number of elements. This value is stored in the variable
n. The function summation_square is called with n as an argument. The returned value of the function is printed with message “Square of the summation of n numbers is “.
As before, it’s always a good practice to validate the input, in this case, make sure the entered number of elements is not negative or zero.
If you liked this post, then please share this with your friends and make sure to bookmark this website for more awesome content.