Python Program to Find Sum of N Numbers

We have to develop a Python program to find the sum of n numbers. We will give some numbers and the python program will add these numbers using various methods(like for loop, while loop). We have also developed a program using built-in function sum() methods to adding numbers.

Sum of N Numbers in Python using For Loop

This Python program is the simplest and easiest way to calculate the sum of n numbers. First, we defined the total number we want to enter in inputs. Then, we will take numbers and calculate the total sum of those numbers using the For Loop. Finally, the result will be displayed on the screen.

# Python program to find sum of n numbers

# total number you want to enter
n = int(input('How many numbers: '))

# denotes total sum of n numbers
total_sum = 0

for i in range (n):
    # take inputs
    num = float(input('Enter number: '))
    # calculate total sum of numbers
    total_sum += num

# print sum of numbers
print('The sum of numbers = %0.2f' %total_sum)

Output for the different input values:-

How many numbers: 3
Enter number: 10
Enter number: 5
Enter number: 3
The sum of numbers = 18.00

How many numbers: 5
Enter number: 8
Enter number: 4
Enter number: 13
Enter number: 9
Enter number: 25
The sum of numbers = 59.00

Sum of N Numbers in Python using While Loop

In the previous program, we used for loop to calculate the sum of n numbers but in this program, we are using the While Loop to find the sum of n numbers.

# Python program to find sum of n numbers

# total number you want to enter
n = float(input('How many numbers: '))

# denotes total sum of n numbers
total_sum = 0

i =1
while i <= n:
    # take inputs
    num = float(input('Enter number: '))
    # calculate total sum of numbers
    total_sum += num
    
    i = i+1

# print sum of numbers
print('Average of numbers = %0.2f' %total_sum)

Output:-

How many numbers: 2
Enter number: 8
Enter number: 2.6
The sum of numbers = 10.60

Sum of N Numbers using Function

This python program also performs the same task but with different methods. In this program, we can also take the help of a function to find the sum of numbers. A function is a block of code that performs a specific task. In Python, we can find the sum of numbers by simply using the sum() function. The Python sum() method is a built-in method that returns the summation of all the elements of the passed iterable.

# Python program to find sum of n numbers

def add(arr):
    # calculate total sum of numbers
    return sum(arr)

# total number you want to enter
n = int(input('How many numbers: '))

# take inputs
num = []
for i in range(n):
    num.append(float(input('Enter number: ')))

# calling function and print sum of numbers
print('The sum of numbers = %0.2f' %add(num))

Output:-

How many numbers: 3
Enter number: 20
Enter number: 10
Enter number: 5
The sum of numbers = 35.00

Also See:- Python program to find the sum of N natural numbers

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply