Codeforcoding

Python: Average of Odd & Even Numbers from User Input

Python: Average of Odd & Even Numbers from User Input

In this post, we will realize about the title of  “Python: Average of Odd & Even Numbers from User Input”.

In this article, we will explore how to calculate the average of odd and even numbers in a list. The list will be generated from user input, and we will use both for-loops and while-loops to achieve this.

Python: Average of Odd & Even Numbers from User Input
Calculate Average

Here’s a Python program that calculates the average of odd and even numbers

Program 1

This program calculates the average of odd and even numbers in a list using a for-loop.

Program 1

# Python code to calculate Average of odd and even numbers in a list
# Input list from user
NumList = list(map(int, input("Enter numbers separated by spaces\n").split()))

# Initialize variables
even_Sum = 0
even_Count = 0
odd_Sum = 0
odd_Count = 0

# Loop through the list
for num in NumList:
    if num % 2 == 0:
        even_Sum += num  # Add to even sum
        even_Count += 1
    else:
        odd_Sum += num  # Add to odd sum
        odd_Count += 1

# Calculate average
even_Avg = even_Sum / even_Count if even_Count != 0 else 0
odd_Avg = odd_Sum / odd_Count if odd_Count != 0 else 0

# Print the result
print(f"Average of even numbers: {even_Avg}")
print(f"Average of odd numbers: {odd_Avg}")

 

When the above code is executed, it produces the following result

Enter numbers separated by spaces
5 6 7 8 9 10 11 12 13 14 15
Average of even numbers:10.0
Average of odd numbers:10.0

 

 

Program 2

This program calculates the average of odd and even numbers in a list using a while-loop.

# Python code to calculate Average of odd and even numbers in a list
# Input list from user
NumList = list(map(int, input("Enter numbers separated by spaces\n").split()))

# Initialize variables
even_Sum = 0
even_Count = 0
odd_Sum = 0
odd_Count = 0
index = 0

# Loop through the list
while index < len(NumList):
    if NumList[index] % 2 == 0:
        even_Sum += NumList[index]  # Add to even sum
        even_Count += 1
    else:
        odd_Sum += NumList[index]  # Add to odd sum
        odd_Count += 1
    index += 1

# Calculate average
even_Avg = even_Sum / even_Count if even_Count != 0 else 0
odd_Avg = odd_Sum / odd_Count if odd_Count != 0 else 0

# Print the result
print(f"Average of even numbers: {even_Avg}")
print(f"Average of odd numbers: {odd_Avg}")

 

When the above code is executed, it produces the following result

Enter numbers separated by spaces
11 12 13 14 15 16
Average of even numbers:14.0
Average of odd numbers:13.0

 

 

Explore Similar Programs

Looking for more examples? Check out these programs in various languages:

C language

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++ language

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 language

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 language

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

for loop in Python

while loop in Python

If statements in Python

 

 

 

Write a function or method to convert C into F -Entered by user
Python: Calculate Average of odd and even in a list using loops
Exit mobile version