need help with outputting only even numbers on python
SajidVijayan3 (1)

Ask the user for 5 integers and store them in a list.
Find and output all its elements with even indices (i.e. A[0], A[2], A[4], ...) on the same line with a space between them.

You are viewing a single comment. View All
PYer (2532)

Use the % operator to find the remainder, and in this case if a number is even. Run a for loop and then have it append the even items to a list. Then you can use the join() method to print them all. In this, I am assuming you are using python.

n1 = [1, 2, 3, 4, 5] # change this to user input as a list
n2 = []
for n in n1:
    if n1.index(n) % 2 == 0: # If it is even, meaning the remainder is 0 when divided by 2
        n2.append(n)
# Use join method to join them all into a string
nums = ' '.join(n2)
print(nums)

Please mark this as the answer thanks