Write a program to read n numbers from a user and print : ● Number of positive numbers. ● Number of negative numbers. ● Number of zeros. ● Number of odd numbers. ● Number of even numbers. ● Average of all numbers. (Using match case)

 

n = int(input("Enter the number of elements: "))
numbers = []
print(f"Enter {n} numbers")
for i in range(n):
    num = int(input(""))
    numbers.append(num)

positive_count = 0
negative_count = 0
zero_count = 0
odd_count = 0
even_count = 0
total_sum = 0

for num in numbers:
    match num:
        case num if num > 0:
            positive_count += 1
        case num if num < 0:
            negative_count += 1
        case 0:
            zero_count += 1

    match num % 2:
        case 0:
            even_count += 1
        case _:
            odd_count += 1

    total_sum += num

average = total_sum / n if n != 0 else 0

print(f"Number of positive numbers: {positive_count}")
print(f"Number of negative numbers: {negative_count}")
print(f"Number of zeros: {zero_count}")
print(f"Number of odd numbers: {odd_count}")
print(f"Number of even numbers: {even_count}")
print(f"Average of all numbers: {average:.2f}")



Output:
Enter the number of elements: 12 Enter 12 numbers 1 2 -1 -2 0 1 12 7890 00 -235 12 4 Number of positive numbers: 7 Number of negative numbers: 3 Number of zeros: 2 Number of odd numbers: 4 Number of even numbers: 8 Average of all numbers: 640.33

Comments