codescracker
python

Python Program to Count Even and Odd Numbers in a List



« Previous Program Next Program »

This article is created to cover some programs in Python, that counts total number of even numbers and odd numbers available in a given list entered by user at run-time. Here are the list of programs covered in this article:

Count Even/Odd in a List of 10 Elements

The question is, write a Python program to count total number of even and odd numbers present in a list of 10 numbers entered by user. Here is its answer:

nums = []
totEven = 0
totOdd = 0

print("Enter 10 Numbers: ")
for i in range(10):
  nums.insert(i, int(input()))

for i in range(10):
  if nums[i]%2==0:
    totEven = totEven+1
  else:
    totOdd = totOdd+1

print("\nEven Number: ")
print(totEven)
print("Odd Number: ")
print(totOdd)

Here is its sample run:

python count even odd numbers in list

Now supply the input as any 10 numbers say 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 and then press ENTER key to count and print how many even and odd numbers available in the given list as shown in the snapshot given below:

count even odd numbers in list python

Count Even/Odd Numbers in List of n Elements

This program allows user to define the size of list. The question is, write a Python program to count even/odd numbers in a list of n size. The following program is the answer to this question:

nums = []
totEven = 0
totOdd = 0

print(end="Enter the Size: ")
size = int(input())

print(end="Enter " +str(size)+ " Numbers: ")
for i in range(size):
  nums.insert(i, int(input()))

for i in range(size):
  if nums[i]%2==0:
    totEven = totEven+1
  else:
    totOdd = totOdd+1

print("\nEven Number: " +str(totEven))
print("Odd Number: " +str(totOdd))

Here is its sample run with user input 5 as size, and 1, 2, 3, 4, 5 as five numbers:

count even odd numbers in list python program

Python Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker