Python program to count numbers of even and odd elements in an array

Get Prepinsta Prime

Get all 200+ courses offered by Prepinsta

Never Miss an OffCampus Update

Get OffCampus Updates on Social Media from PrepInsta

Follow us on our Media Handles, we post out OffCampus drives on our Instagram, Telegram, Discord, Whatsdapp etc.

Get Hiring Updates
Amazon,Google,Delottie & 30+companies are hiring ! Get hiring Updates right in your inbox from PrepInsta

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others.

Get PrepInsta Prime Subscription

Get access to all the courses that PrepInsta offers, check the out below -

Companies

TCS, Cognizant, Delloite, Infosys, Wipro, CoCubes, KPMG, Amazone, ZS Associates, Accenture, Congnizant & other 50+ companies

Programming

Data Structures, Top 500 Codes, C, C++, Java Python & other 10+ subjects

Skills

Full Stack Web Development, Data Science, Machine Learning, AWS Cloud, & other 10+ skills and 20+ projects

OffCampus Updates

Never Miss OffCampus Updates

Get OffCampus Update from PrepInsta

Follow us on our Social Media Handles

count even and odd elements in python

Count number of even and odd elements in Python

Here, in this page you will find the program to count number of even and odd elements in python programming language. We are given with an integer array and need to print the count of even elements and odd elements.

Here, we will discuss the following two methods to print the count of even elements and odd elements.

  • Method 1 : Using Modulo operator
  • Method 2 : Using Bit-wise AND operator.

Let’s discuss above two methods one by one,

Method 1 :

  • Take two variables, countEven=0, countOdd=0.
  • Iterate over the array,
  • Increment countEven if(arr[i]%2==0)
  • Otherwise increment the value of countOdd by 1.

Method 1 : Code in Python

arr = [1, 7, 8, 4, 5, 16, 8]
n = len(arr)
countEven = 0
countodd = 0

for i in range(0, n):
if arr[i]%2==0 :
countEven += 1
else:
countodd += 1

print("Even Elements count : " )
print(countEven)

print("Odd Elements count : ")
print(countodd)

Output :

Even Elements count :
4
Odd Elements count :
3

Method 2 :

In this method we will use bit-wise AND operator. By doing AND of 1 with array element, if the result comes out to be 0 then the number is even otherwise odd.

Method 2 : Code in Python

arr = [1, 7, 8, 4, 5, 16, 8]
n = len(arr)
countEven = 0
countodd = 0

for i in range(0, n):
if arr[i]&1==0 :
countEven += 1
else:
countodd += 1

print("Even Elements count : " )
print(countEven)

print("Odd Elements count : ")
print(countodd)

Output :

Even Elements count :
4
Odd Elements count :
3

Comments

One comment on “Python program to count numbers of even and odd elements in an array”


  • Sharvari

    Python Code :
    size = int(input(“Enter the Array Size : “))
    arr = []
    for i in range(size):
    element = int(input())
    arr.append(element)
    print(arr)

    x = list(dict.fromkeys(arr))
    print(x)