Python Program To Perform Arithmetic Operation in Array
def perform_arithmetic_operations(arr):
# Addition
sum_result = sum(arr)
# Subtraction
subtract_result = arr[0]
for num in arr[1:]:
subtract_result -= num
# Multiplication
multiply_result = 1
for num in arr:
multiply_result *= num
# Division
divide_result = arr[0]
for num in arr[1:]:
if num != 0: # Avoid division by zero
divide_result /= num
else:
print("Error: Division by zero!")
return
return sum_result, subtract_result, multiply_result, divide_result
# Initialize an empty array
array_of_numbers = []
# Get the number of elements you want in the array from the user
num_elements = int(input("Enter the number of elements you want in the array: "))
# Loop to take user input and add numbers to the array
for i in range(num_elements):
# Get the number from the user
user_input = float(input(f"Enter element {i + 1}: "))
# Add the number to the array
array_of_numbers.append(user_input)
result = perform_arithmetic_operations(array_of_numbers)
# Display the results
print(f"Sum: {result[0]}")
print(f"Subtraction: {result[1]}")
print(f"Multiplication: {result[2]}")
print(f"Division: {result[3]}")
Output:
Enter the number of elements you want in the array: 5
Enter element 1: 23
Enter element 2: 12
Enter element 3: 43
Enter element 4: 76
Enter element 5: 34
Sum: 188.0
Subtraction: -142.0
Multiplication: 30666912.0
Division: 1.724986200110399e-05