In this tutorial, we will discuss about separating odd and even numbers in a list into two different list.
When you divide a number by two and if the balance is zero, it is an even number.
When a number is divided by two with a remaining balance of 1, then it’s an odd number.
Example of even number 2,4,6,8,…..
Example of odd number 1,3,5,7,…..
Here, we will use a modular operator to display odd or even numbersFirst.
if n%2==0, n is a even number
if n%2==1, n is a odd number
Program 1
Approach
if n%2==0, n is an even number, it is saved into an evenList=[]
if n%2==1, n is an odd number, it is saved into an oddList=[]
Program
numbers=[] #Create a empty list for store number from user input num=int(input("Enter number of elements: ")) for i in range(1,num+1): listElements=int(input("Enter telement %d: "%i)) numbers.append(listElements) evenList=[] #list for store even number oddList=[] #list for store odd number for j in numbers: if j%2==0: evenList.append(j) else: oddList.append(j) print("Even number list: ",evenList)#display separated even number list print("Odd number list: ",oddList) #display separated odd number list
When the above code is executed, it produces the following results
Enter number of elements: 7 Enter telement 1: 25 Enter telement 2: 46 Enter telement 3: 67 Enter telement 4: 78 Enter telement 5: 89 Enter telement 6: 90 Enter telement 7: 87 Even number list: [46, 78, 90] Odd number list: [25, 67, 89, 87]
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
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…