Hello everyone, welcome to nkcoderz.com. In this article we will going to discuss about Python program to find the summation of n numbers.
Table of Contents
Code for python program to find the summation of n numbers
Here is an example of a Python program that calculates the summation of a given number of numbers:
def summation(n):
sum = 0
for i in range(1, n+1):
num = int(input("Enter a number: "))
sum += num
return sum
n = int(input("Enter the number of elements: "))
print("Summation of", n, "numbers is", summation(n))
Output
Enter the number of elements: 2
Enter a number: 1
Enter a number: 2
Summation of 2 numbers is 3
Explanation
In this program, the summation()
function takes one argument: a number n. The function uses a for
loop to repeatedly ask the user to enter a number, and then add that number to the current sum. At the end of the loop, the function returns the total sum of the numbers.
The program starts by asking the user to enter the number of elements. This value is stored in the variable n
. The function summation is called with n as an argument. The returned value of the summation is printed with message “Summation of n numbers is “.
You can test the program by running it and providing different numbers as inputs.
It is good to practice to validate the input, in this case make sure the entered number of elements is not negative or zero.
Python program to print the number of days in a month using dictionary
Conclusion
If you liked this post, then please share this with your friends and make sure to bookmark this website for more awesome content.