write a programme in python to input a list cantaing a number and calculate the sum of all ODD element present inside the list? if list is (1,2,3,4,5,6,7) then sum =1+3+5+7+=16
Answers
Answer:
sum=0;
number=(1,2,3,4,5,6,7);
for i in number:
if (i%2)==1:
sum=sum+i;
print (sum);
Answer:
n=int(input("enter the max no of elements you want to enter"))
lis1=[0]*n
even=0
odd=0
for i in range(n):
lis1[i]=int(input("Enter the elements"))
print("Elements in the list are",lis1)
for i in range(n):
if (lis1[i]%2==0):
even=even+lis1[i]
else:
odd=odd+lis1[i]
print("Sum of even elements are",even)
print("Sum of odd elements are",odd)
Output:
enter the max no of elements you want to enter10
Enter the elements12
Enter the elements15
Enter the elements14
Enter the elements19
Enter the elements16
Enter the elements18
Enter the elements17
Enter the elements1
Enter the elements2
Enter the elements3
Elements in the list are [12, 15, 14, 19, 16, 18, 17, 1, 2, 3]
Sum of even elements are 62
Sum of odd elements are 55
Explanation:
1. n variable is created to store length of the list
2. A list is created with same length
3. Using for loop elements are taken from the user and inserted in the list.
4. Using print statement elements are displayed.
5. condition statements are used to differentiate odd and even numbers.
6. modulus operator returns reminder
7. If the element returns 0 when divided by 2 we can confirm it as even number else odd.
8. Inside for loop even numbers and odd numbers are added to the variables even and odd.
9. Display the sum of even and odd numbers using print statement