Enter an array containing 6 numbers and replace the last element with the sum of the array elements that are multiples of 3 — Python(Питон)

Here’s a Python program that prompts the user to enter an array of 6 numbers and replaces the last element with the sum of the array elements that are multiples of 3:

# Prompt the user to enter an array of 6 numbers
array = []
for i in range(6):
    num = int(input("Enter a number: "))
    array.append(num)

# Calculate the sum of array elements that are multiples of 3
sum_multiples_of_3 = 0
for num in array:
    if num % 3 == 0:
        sum_multiples_of_3 += num

# Replace the last element with the sum
array[-1] = sum_multiples_of_3

# Print the updated array
print("Updated array:", array)

In this program, we use a loop to prompt the user to enter 6 numbers and store them in the array list. Then, we iterate over the elements of the array and check if each element is a multiple of 3 using the modulus operator %. If an element is a multiple of 3, we add it to the sum_multiples_of_3 variable. Finally, we replace the last element of the array with the calculated sum and print the updated array.

Leave a Comment