Hi Guys, welcome to my blog in this article you will learn about how to Write A Python Program To Add N Numbers Accepted From The User
Title :
Write A Python Program To Add N Numbers Accepted From The User
Description :
Hi guys, in this article you will learn a python program to accept a number from the user and calculate the sum of all the n numbers that you have accepted from the user and then print it on to the standard.
First take a length variable and ask a user to enter the count of numbers, then take an empty list of numbers and initialize it.
Now using iterate from 0 to the in numbers, from the user to enter a number and then appended to the list of numbers.
This is the method to accept a numbers and the user
length = int(input("Enter The Count of Numbers : "))
numbers = []
for index in range(length):
temp = int(input(f"Enter {index+1} Number : "))
numbers.append(temp)
take a total variable and initialize it to zero, this variable will be used to store the sum of all the numbers. use the for loop to iterate all the items present in the numbers list and add it to the total variable.
total = 0
for item in numbers:
total += item
Finally use the print function call to print the sum of all the n numbers accepted from the user.
print(f"Addition of All the n Numbers is : {total}")
Complete Python Code :
length = int(input("Enter The Count of Numbers : "))
numbers = []
for index in range(length):
temp = int(input(f"Enter {index+1} Number : "))
numbers.append(temp)
total = 0
for item in numbers:
total += item
print(f"Addition of All the n Numbers is : {total}")
Output :
Enter The Count of Numbers : 5
Enter 1 Number : 67
Enter 2 Number : 90
Enter 3 Number : 45
Enter 4 Number : 34
Enter 5 Number : 55
Addition of All the n Numbers is : 291
Output :
Enter The Count of Numbers : 3
Enter 1 Number : 1
Enter 2 Number : 2
Enter 3 Number : 3
Addition of All the n Numbers is : 6
Conclusion :
The above Python program accepts a number from the user and appends it to an empty list of numbers. Iterates all the numbers present in the list using the for loop and calculates the sum and finally prints it on to the standard output.
Comment it down below if you have any suggestions to improve the above Python program
0 Comments