You are on page 1of 2

Student Name:

Student Number:
CS 104 – Spring 2022

QUIZ 3
1. (50pts) Write a program that finds the sum of a set of numbers, and how
many of the values in the set are odd numbers. The program should
prompt for the number of elements in the set; and then it should prompt
for each number in the set from a user (you need to use input() function to
get them from the user). The program should print out the sum and
number of odd values in the set as an output. (Hint: odd numbers have a
remainder of 1 when divided by 2; and you can use if statement to check
whether the number is odd, or not).

numOfElements = int( input("How many numbers are there in the set? "))
sum = 0
countOddNums = 0

#prompt as many numbers as numOfElements, add them together and


#count the number of odd numbers.
for i in range(numOfElements):
nextNumber = int(input("What is the next number?"))
sum += nextNumber

#check if the number is odd


if nextNumber % 2 == 1:
countOddNums += 1

print("The Sum of ", numOfElements, " numbers = ", sum)


print("The number of odd values = ", countOddNums)

2. (50pts) What output is produced by the following program?

def mystery(x, y, z):


if x > y:
print("y = ", y, "x = ", x)
elif x > z:
print("z = ", y, "x = ", x)
if y > z:
print("z = ", z, "y = ", y)
elif z > x:
print("x = ", x, "z = ", z)
else:
print("x = ", x, "y = ", y , "z = ", z)

mystery(3,4,5) # a
mystery(5,4,3) # b
mystery(5,3,4) # c
mystery(3,5,4) # d
mystery(4,3,5) # e
a)
x = 3 z = 5

b)
y = 4 x = 5
z = 3 y = 4

c)
y = 3 x = 5
x = 5 y = 3 z = 4

d)
z = 4 y = 5

e)
y = 3 x = 4
x = 4 z = 5

You might also like