175k views
19 votes
9.18 lab: even/odd values in a vector write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither. the input begins with an integer indicating the number of integers that follow. ex: if the input is 5 2 4 6 8 10, the output is: all even ex: if the input is 5 1 3 5 7 9, the output is: all odd ex: if the input is 5 1 2 3 4 5, the output is: not even or odd your program must define and call the following two functions. isvectoreven returns true if all integers in the array are even and false otherwise. isvectorodd returns true if all integers in the array are odd and false otherwise. bool isvectoreven(vector myvec) bool isvectorodd(vector myvec)

User WayneOS
by
4.9k points

1 Answer

11 votes

def is_list_even(my_list):

for i in my_list:

if(i%2 != 0):

return False

return True

def is_list_odd(my_list):

for i in my_list:

if(i%2 == 0):

return False

return True

def main():

n = int(input())

lst = []

for i in range(n):

lst.append(int(input()))

if(is_list_even(lst)):

print('all even')

elif(is_list_odd(lst)):

print('all odd')

else:

print('not even or odd')

if __name__ == '__main__':

main()

User Plague
by
5.8k points