~2 min read • Updated Sep 17, 2025
Program Overview
This Python program reads a list of numbers from the user and stores them in an array.
It then attempts to divide all elements by the middle element.
If the middle element is zero, it searches forward for the next non-zero element.
If no non-zero element is found, it prints a message indicating that division is not possible.
Python Code:
n = int(input("Enter the number of elements: "))
arr = []
for i in range(n):
num = float(input(f"Enter number {i+1}: "))
arr.append(num)
# Find the divisor
mid_index = n // 2
divisor = None
for i in range(mid_index, n):
if arr[i] != 0:
divisor = arr[i]
break
if divisor is None:
print("All elements are zero. Division is not possible.")
else:
print(f"Dividing all elements by {divisor}:")
result = [x / divisor for x in arr]
print(result)
Sample Output:
Enter the number of elements: 5
Enter number 1: 10
Enter number 2: 20
Enter number 3: 0
Enter number 4: 40
Enter number 5: 50
Dividing all elements by 40:
[0.25, 0.5, 0.0, 1.0, 1.25]
Explanation:
Here’s how the program works:
- It reads n numbers into a list
- It checks the middle element. If it’s zero, it searches forward for a non-zero value
- If a valid divisor is found, all elements are divided by it
- If all elements are zero, a message is printed instead
Written & researched by Dr. Shahin Siami