Check value

Calculate sum of odd and even numbers in a list in Python

Calculate sum of odd and even numbers in a list in Python

In this tutorial, we will discuss ThePython 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 program

 

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

 

Calculating the sum of odd and even numbers using for loop

Program 1

This program allows the user to choose any value for list length(list elements) and input values into the list.  The program will calculate the sum of odd and even numbers from the list using “for loop”.

#Python program to find sum of odd and even numbers in a list
NumList=[]       #empty list
evenSum=0        #declare and initialised variable evenSum to sum of even numbers
oddSum=0         #declare and initialised variable oddSum to sum of odd numbers

Number=int(input("Please enter the total number of list elements: "))
for i in range(1, Number+1):
    value=int(input("Please enter the value of %d Eleements: " %i))
    NumList.append(value)

for j in range(Number):
    if(NumList[j]%2==0):
        evenSum=evenSum+NumList[j]
    else:
        oddSum=oddSum+NumList[j]
print("\n The sum of even numbers in the list= ",evenSum)
print("\n The sum of odd numbers in the list= ",oddSum)

When the above code is executed, it produces the following results

Please enter the total number of list elements: 6
Please enter the value of 1 Eleements: 12
Please enter the value of 2 Eleements: 21
Please enter the value of 3 Eleements: 23
Please enter the value of 4 Eleements: 32
Please enter the value of 5 Eleements: 34
Please enter the value of 6 Eleements: 43
('\n The sum of even numbers in the list= ', 78)
('\n The sum of odd numbers in the list= ', 87)

 

Method

To begin with , defines a list without including the elements named as NumList=[].

Next, declares and initializes two variables: oddSum=0, evenSum=0

Then, receives input from the user regarding the list length.

Afterwards, the elements for a list is received from the user.

Then, using the “for loop”, the elements are added one by one to the list.

Then, using the second “for loop”, elements are picked one by one from the list to determine oddness and evenness.

Next, “if statements” are used to find a number and then if it is even, it is added to evenSum.If the number is not even, it is dealt with by  “else statement”.

After that, the “else statement” adds the element to oddSum.

Finally, the sums of odd numbers and even numbers are displayed.

 

 

Calculating the sum of odd and even numbers using “while loop”

Program

This program allows the user to choose any value for list length and input values into the list.  The program will calculate the sum of odd and even numbers from the list using “while loop”.

#Python program to find sum of Even and Odd number in a list

numList=[]        #create empty list for entering number
evenSum=0       #Declare and initialise a variable as evenSum=0
oddSum=0        #Declare and initialise a variable as oddSum=0

num=int(input("Enter the number of list elements"))
for i in range(1, num+1):
    Value=int(input("Please enter the value %d : " %i))
    numList.append(Value)

    j=0
while(j<num):
    if(numList[j] %2 ==0):
        evenSum=evenSum+numList[j]
    else:
        oddSum=oddSum+numList[j]
    j=j+1

print("The sum of Even Numbers in this list =",evenSum)
print("The sum of odd Numbers in this list =",oddSum)

When the above code is executed, it produces the following results

Enter the number of list elements6
Please enter the value 1 : 12
Please enter the value 2 : 21
Please enter the value 3 : 23
Please enter the value 4 : 32
Please enter the value 5 : 34
Please enter the value 6 : 43
('The sum of Even Numbers in this list =', 78)
('The sum of odd Numbers in this list =', 87)

 

Method:

To begin with , declares and initializes two variables: oddSum=0, evenSum=0

Next, receives input from the user regarding the list length.

Then, defines a list without including the elements  named as numList=[].

Afterwards, the elements for a list is received from the user.

Then, using the “for loop”, the elements are added one by one to the list.

Then, using the  “while loop”, elements are picked one by one from the array to determine oddness and evenness.

Next, “if statements” are used to find a number and then if it is even, it is added to evenSum.If the number is not even, it is dealt with by  “else statement”.

After that, the “else statement” adds the element to oddSum.

Finally, the sums of odd numbers and even numbers are displayed.

 

Similar post

C program to find a number is even or odd using the function

C program to separate Odd and Even numbers from an array

C program to Electricity bill calculation using the function

C program to display all even and odd numbers from 1 to n

C program display odd and even numbers without if statements

C program to calculate the sum of odd and even numbers

C program to find whether a number is even or odd

 

C++ program to find a number is even or odd using the function

C++ program to separate Odd and Even numbers from an array

C++ program to display all even and odd numbers from 1 to n

C++ program calculate Average of odd and even numbers in an array 

C++ program to calculate the sum of odd and even numbers

C++ program to find whether a number is even or odd

 

Java program to find a number is even or odd using the method

Java program to separate Odd and Even numbers from an array

Java program to display all even and odd numbers from 1 to n

Java program display odd and even numbers without if statements

Java program to calculate the sum of odd and even numbers

Java program to find whether a number is even or odd

 

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

for loop in Python

while loop in Python

If statements in Python

 

Calculate sum of odd and even of an array in C
Java code to Calculate average of odd and even in an array
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

1 day ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago