In this, you'll get to learn how to find the sum of n numbers in python (using different-different approaches) where the value of n is entered by a user at the runtime with n numbers. i.e. Let's say if the user entered 4, the prompt should ask to enter 4 numbers. If the user enters 4 numbers -- let's say 5, 10, 15, 20 then the output should be 50
(5+10+15+20=50).
This covers four different approaches:
This is an example to find the sum of n numbers using for loop. Where the value of n and then n numbers must be entered by the user at the runtime. If the user enters the value of n as 5, then this block of code must be executed 5 times to get 5 numbers. After 5 numbers are received all numbers are added and initialized to sum, one by one.
sum = 0
n = int(input("Enter the Value of n: "))
print(f"Enter {n} numbers (type a number and press ENTER): ")
for i in range(n):
num = int(input())
sum = sum+num
print(f"The sum of {n} numbers = {sum}")
Here, the f-string is a string literal that is used as an f at the beginning and curly braces containing expression that will be replaced with its value.
Output:
Enter the Value of n: 5
Enter 5 numbers(type a number and press ENTER):
55
20
45
75
20
The sum of 5 numbers = 215
This example is to find the sum of numbers that is the same as above but using while loop instead.
sum = 0
n = int(input("Enter the Value of n: "))
print(f"Enter {n} numbers (type a number and press ENTER): ")
i = 0
while i < n:
num = int(input())
sum = sum+num
i += 1
print(f"The sum of {n} numbers = {sum}")
Output:
Enter the Value of n: 5
Enter 5 numbers(type a number and press ENTER):
55
20
45
75
20
The sum of 5 numbers = 215
This example is to find the sum of numbers in Python using the list. In this, every input number is added into a list (num_list) one by one. Then the numbers in a num_list are added using for loop.
num_list = []
sum = 0
n = int(input("Enter the Value of n: "))
print(f"Enter {n} numbers (type a number and press ENTER): ")
for i in range(n):
num_list.insert(i, int(input()))
for i in range(n):
sum += num_list[i]
print(f"The sum of {n} numbers = {sum}")
Here, +=
in sum += num_list[i]
represents the addition assignment(+=) operator, and that line is equivalent to sum = sum + num_list[i]
.
Output:
Enter the Value of n: 5
Enter 5 numbers (type a number and press ENTER):
55
20
45
75
20
The sum of 5 numbers = 215
This also does the same task as the above programs but using the function method.
def Summation(n_list, N):
result = 0
for i in range(N):
result += n_list[i]
return result
num_list = []
n = int(input("Enter the Value of n: "))
print(f"Enter {n} numbers (type a number and press ENTER): ")
for i in range(n):
num_list.insert(i, int(input()))
sum = Summation(num_list, n)
print(f"The sum of {n} numbers = {sum}")
Here, the function Summation()
takes two arguments -- number list and the value of n, then it executes using for loop to find the sum of individual items in the list. The sum of all the numbers in the list is stored in result
variable which is the desired result (summation of n numbers entered by the user).
Output:
Enter the Value of n: 5
Enter 5 numbers (type a number and press ENTER):
55
20
45
75
20
The sum of 5 numbers = 215
In this, we discussed different examples for different methods to find the sum of n numbers in Python -- using for loop
, using while loop
, using a function
, and using list
.