Python program to calculate the sum of odd and even numbers in a list
Python program to calculate the sum of odd and even numbers in a list
In this tutorial, we will discuss the Python program to calculate the sum of odd and even numbers in a list
In this article, we are going to learn the concept of how to calculate the sum of odd and even numbers in the Python programming language
What is even or odd numbers?
When any integer value which ends in 0,2,4,6,8 is divided by two it is called as an even number
Example for even numbers – 34,-64,78,788
When any integer value which ends in 0,1,3,5,7,9 is not divided by two it is called as an odd number
Example for odd numbers – 33,-69,75,785
Here, we can use a modular operator to find odd or even number in the list
if n%2==0 n is an even number
if n%2==1 n is an odd number
Python program to Calculate Addition of odd and even
Calculating the sum of odd and even numbers using for loop
Program 1
In this program, the user prompts the program to add some numbers to the list, and then the program calculates the sum of odd and even numbers from the list using the “for loop.”
# Python code to Calculate sum of odd and even in a list using while loop #input list NumList=list(map(int, input("Enter numbers separated by spaces: ").split())) #initialize sums even_Sum=0 odd_Sum=0 #Loop through the list for num in NumList: if num%2==0: even_Sum+=num #Add to even sum else: odd_Sum+=num #Add to odd sum #print the result print(f"Sum of even numbers:{even_Sum}") print(f"Sum of even numbers:{odd_Sum}")
When the above code is executed, it produces the following results
Enter numbers separated by spaces: 5 6 7 8 9 10 11 12 13 14
Sum of even numbers:50
Sum of even numbers:45
Method
- To begin with, define a list without including the elements named NumList=[].
- The program prompts to add some numbers to the list from the user
- Next, declare and initialize two variables: odd_Sum=0, even_Sum=0
- Then, using the “for loop”, elements are picked individually from the list to determine odd and even numbers
- Next, “if statements” are used to find a number, and then, if it is even, it is added to the variable evenSum.If the number is not even, it is dealt with by an “else statement”.
- After that, the “else statement” adds the element to the variable oddSum.
- Finally, the sums of odd numbers and even numbers are displayed.
Calculating the sum of odd and even numbers using “while loop”
In this program, the user prompts the program to add some numbers to the list, and then the program calculates the sum of odd and even numbers from the list using the “=while loop.”
Program 2
# Python code to Calculate sum of odd and even in a list using while loop #input list num=list(map(int, input("Enter numbers separated by spaces: ").split())) #initialized variable even_Sum=0 odd_Sum=0 index=0 #Loop through the list using while while index<len(num): if num[index]%2==0: even_Sum+=num[index] else: odd_Sum+=num[index] index+=1 print(f"Sum of even numbers: {even_Sum}") print(f"Sum of odd numbers: {odd_Sum}")
When the above code is executed, it produces the following results
Enter numbers separated by spaces: 3 6 9 12 15 18 21 24 27 30
Sum of even numbers: 90
Sum of odd numbers: 75
Method:
- To begin with, define a list without including the elements named NumList=[].
- The program prompts to add some numbers to the list from the user
- Next, declare and initialize two variables: odd_Sum=0, even_Sum=0
- Then, using the “while loop”, elements are picked individually from the list to determine odd and even numbers
- Next, “if statements” are used to find a number, and then, if it is even, it is added to the variable evenSum.If the number is not even, it is dealt with by an “else statement”.
- After that, the “else statement” adds the element to the variable oddSum.
- Finally, the sums of odd numbers and even numbers are displayed.
Similar post
Python program check whether a number is odd or even
Python program to check a number is even or odd using the function
Python program to display even and odd numbers without if
Python program to display even and odd number in the given range
Separate odd and even numbers in a list to different two list
Python Program to find odd and even numbers from a list
Suggested for you