This is a Python Program to print the sum of negative numbers, positive even numbers and positive odd numbers in a given list.
The program prints the sum of negative numbers, positive even numbers and positive odd numbers in a given list..
1. Take in the number of elements to be in the list from the user.
2. Take in the elements from the user using a for loop and append to a list.
3. Using a for loop, get the elements one by one from the list and check if it is positive or negative.
4. If it is positive, check if it is odd or even and find the individual sum.
5. Find the individual sum of negative numbers.
6. Print all the sums.
7. Exit.
Here is source code of the Python Program to print the sum of negative numbers, positive even numbers and positive odd numbers in a given list. The program output is also shown below.
n=int(input("Enter the number of elements to be in the list:")) b=[] for i in range(0,n): a=int(input("Element: ")) b.append(a) sum1=0 sum2=0 sum3=0 for j in b: if(j>0): if(j%2==0): sum1=sum1+j else: sum2=sum2+j else: sum3=sum3+j print("Sum of all positive even numbers:",sum1) print("Sum of all positive odd numbers:",sum2) print("Sum of all negative numbers:",sum3)
1. User must enter the number of elements to be in the list.
2. User must then enter the elements of the list one by one which is appended to the list.
3. The for loop is used to traverse through the list and obtain the elements one by one.
4. The if statement checks whether the element is positive or negative.
5. If it is positive, another if statement checks if it is odd or even.
6. The sum of even elements is stored in sum1 and the sum of odd elements is stored in sum2.
7. The sum of negative numbers is stored in sum3.
8. All the sums are respectively printed.
Case 1: Enter the number of elements to be in the list:4 Element: -12 Element: 34 Element: 35 Element: 89 Sum of all positive even numbers: 34 Sum of all positive odd numbers: 124 Sum of all negative numbers: -12 Case 2: Enter the number of elements to be in the list:5 Element: -45 Element: -23 Element: 56 Element: 23 Element: 7 Sum of all positive even numbers: 56 Sum of all positive odd numbers: 30 Sum of all negative numbers: -68
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
- Apply for Python Internship
- Check Information Technology Books
- Practice Programming MCQs
- Apply for Programming Internship
- Check Python Books