In this article, you will learn and get code find the sum of n numbers entered by user using a Python program. Here are the list of programs:
For example, if user enters the value of n as 3 and then three numbers as 1, 2, 3. Then the answer will be 1+2+3 or 6.
This program finds the sum of n numbers using for loop. Here the value of n and then n numbers must be entered by user as shown in the program given below:
# Find Sum of n Numbers using for Loop # ----codescracker.com---- sum = 0 print("Enter the Value of n: ") n = int(input()) print("Enter " + str(n) + " Numbers: ") for i in range(n): num = int(input()) sum = sum+num print("Sum of " + str(n) + " Numbers = " + str(sum))
Here is its sample run:
Now supply the input say 6 as value of n and then enter 6 numbers say 10, 20, 30, 40, 50, 60. Here is the sample output with exactly these inputs:
Note - The str() is used to convert from integer to string. Because the + operator in python is used to concatenate only same type's value.
The following code:
for i in range(n):
is used to execute the following block of code:
num = int(input()) sum = sum+num
n number of times starting with value of i from 0 to n-1. For example, if user enters the value of n as 10, then this block of code gets executed 10 times. Using this block, we've received 10 numbers and adds the value and initialized to sum, one by one.
This program does the same job as of previous program, using while loop instead of for:
# ----codescracker.com---- sum = 0 i=0 print(end="Enter the Value of n: ") n = int(input()) print(end="Enter " + str(n) + " Numbers: ") while i<n: num = int(input()) sum = sum+num i = i+1 print("\nSum of " + str(n) + " Numbers = " + str(sum))
Here is its sample run, with user input 10 as value of n and 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 as 10 numbers:
This program find and prints the addition result (or sum) of n numbers entered by user at run-time using list. List is like an array:
# ----codescracker.com---- num = [] sum = 0 print(end="Enter the Value of n: ") n = int(input()) print(end="Enter " + str(n) + " Numbers: ") for i in range(n): num.insert(i, int(input())) for i in range(n): sum = sum+num[i] print("\nSum of " + str(n) + " Numbers = " + str(sum))
Here is its sample run with user input, 5 as value of n and 5, 6, 7, 10, 12 as 5 numbers:
The following code:
for i in range(n):
is used to execute following the statement:
num.insert(i, int(input()))
n number of times with i's value from 0 to n-1. Therefore using insert(), the value entered by user gets inserted to the list num[] one by one in this way:
This is the last program of this article. This program also finds the sum of n numbers as done in previous programs using different-different approaches. Like those, this program also approach in an another way (using a user-defined function) to do the same task:
# ----codescracker.com---- def SumOfNNums(arr, tot): s = 0 for i in range(tot): s = s+arr[i] return s num = [] print(end="Enter the Value of n: ") n = int(input()) print(end="Enter " + str(n) + " Numbers: ") for i in range(n): num.insert(i, int(input())) sum = SumOfNNums(num, n) print("\nSum of " + str(n) + " Numbers = " + str(sum))
Here is its sample run with user input, 3 as value of n and 20, 30, 10 as 3 numbers:
With following statement:
sum = SumOfNNums(num, n)
we've called the function SumOfNNums(). So after executing this statement, the function gets executed and the value return by function is the result, that is the summation of n numbers entered by user. So the return value gets initialized to sum, and its value gets printed.