Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Tuesday, April 16, 2024

Program to find the average of n numbers from the given list.

Code:

def calculate_average(numbers):

    # Check if the list is not empty

    if len(numbers) == 0:

        return None

    # Calculate the average

    average = sum(numbers) / len(numbers)

    return average

# Example usage

user_input = input("Enter a list of numbers separated by spaces: ")

# Convert the input string to a list of floats

numbers_list = [float(x) for x in user_input.split()]

# Calculate the average and display the result

result = calculate_average(numbers_list)

if result is not None:

    print(f"The average of the numbers in the list is: {result}")

else:

    print("The list is empty.")


Output: 





No comments:

Post a Comment