282,758 views
18 votes
18 votes
Lab: even/odd values in an array

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. assume that the list will always contain less than 20 integers.

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. isarrayeven returns true if all integers in the array are even and false otherwise. isarrayodd returns true if all integers in the array are odd and false otherwise.
bool isarrayeven(int inputvals[], int numvals)
bool isarrayodd(int inputvals[], int numvals)

please answer in c

User Falanwe
by
2.8k points

1 Answer

20 votes
20 votes

def input_values(new_list):

new_list = []

n = int(input('Enter number of values: '))

for i in range(n):

new_list.append(int(input('Enter values: ')))

def is_list_even(new_list):

for i in range(len(new_list)):

if new_list[i]%2!=0:

return False

return True

def is_list_odd(new_list):

for i in range(len(new_list)):

if new_list[i]%2==0:

return False

return True

num=input_values()

if is_list_even(num)==True:

print('all even')

elif is_list_odd(num)==True:

print('all odd')

else:

print('not even or odd')

User AyBayBay
by
3.3k points