Hi, in this article lets learn how to Write a Python Program to Count The Number of Even And Odd Numbers From a Series of Numbers.
There are multiple ways to input a series of numbers into the program. The method is used in input function calls and asking a user to enter numbers separated by, or space. then use the map function to convert it into integer and then to a tuple.
Given a series of numbers you have to initialize two counter variables for counting even and odd. Using for loop traverse all the the items present in the series of numbers and check if the item is completely divisible by 2 you then increment the the even counter else increment the word counter.
Python Program to Count The Number of Even And Odd Numbers From a Series of Numbers
series = tuple(map(int, input("Enter Series : ").split(",")))
even_count, odd_count = 0, 0
for item in series:
if item % 2 == 0:
even_count += 1
else:
odd_count += 1
print("Even count in series is : ", even_count)
print("Odd count in series is : ", odd_count)
By the end of the for loop you will be having even counter and odd counter having the count of Even and Odd numbers present in a series of numbers. Print the the Even and Odd counter at the end of the program.
You can also use direct tuple as a series of numbers directly initialize in the program as shown below and still continue to count the number of odd and even in the series of number.
series = (1, 2, 3, 4, 5)
even_count, odd_count = 0, 0
for item in series:
if item % 2 == 0:
even_count += 1
else:
odd_count += 1
print("Even count in series is : ", even_count)
print("Odd count in series is : ", odd_count)
OUTPUT :
# python main.py
Even count in series is : 2
Odd count in series is : 3
Conclusion: Try the program by yourself, comment down below if you have any queries / suggestions.
0 Comments