Program to count even and odd numbers in the given range using Python

by | Mar 11, 2021 | Python Programs

Introduction

In this section, we will count the numbers of even and odd numbers in the given range.

Expression: even: num % 2 == 0

odd: num % 2 != 0

Program to count even and odd numbers in the given range

Program

even_count, odd_count = 0, 0
even_list = []
odd_list = []
n = int(input("Enter the lower bound of range: "))
m = int(input("Enter the upper bound of range: "))
# Using for loop
for i in range(n,m+1):
    if i % 2 == 0:
        even_count += 1
        even_list.append(i)
    else:
        odd_count += 1
        odd_list.append(i)
print("Total even numbers in the range {0} to {1} is {2}".format(n, m,even_count), "and numbers are", even_list)
print("Total odd numbers in the range {0} to {1} is {2}".format(n,m,odd_count), "and numbers are", odd_list)

 

Output

Program to count even and odd numbers in the given range Output

Explanation

In the above python code, we have considered the variable even_count and odd_count to count the number of even’s and odd’s in the range (n , m). The ‘if condition” inside the for loop will check whether the number is even or odd. Each time the even number found, the even_count is increased by 1 and if the odd number is found odd_count is increased by 1. The output is displayed when all the elements are traversed in the given range with the even and odd number.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Go Coding