Python program to add n numbers

Given 'n' integers space separated, add the numbers and print the sum.



Python Implementation:

Using library function:

Output:

Enter 'n' numbers : (space separated)
1 2 3 4 5 6 7 8 9 10
Sum of n numbers is : 55

print "Enter 'n' numbers:(space separated)"
list = map(int,raw_input().split())
print "Sum of n numbers is : ",
print sum(list)

Without using library function:

Output:

Enter the value of 'n': 10
Enter 'n' numbers: (each number in a new line)
1
2
3
4
5
6
7
8
9
10
Sum of n numbers is : 55

print "Enter the value of 'n':"
n = int(raw_input())
sum = 0
print "Enter 'n' numbers:(space separated)"
while n>0:
 line = int(raw_input())
 sum += line
 n-=1
print "Sum of n numbers is : %d"%sum

Using list and without using library function:

Output:

Enter n numbers: (space separated)
1 2 3 4 5 6 7 8 9 10
Sum of n numbers is 55


print "Enter n numbers:(space separated)"
line = map(int,raw_input().split())
sum = 0
for i in range(len(line)):
 sum += line[i]
print "Sum of n numbers is %d"%sum