Lecture Notes on 16 Sep 2009 Binary and Hexadecimal Numbers 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 8 1001 9 1010 A 10 1011 B 11 1100 C 12 1101 D 13 1110 E 14 1111 F 15 * 73 (10) = 1001001 (2) = 111 (8) = 49 (16) * 7AD (16) = 0111 1010 1101 (2) = 1965 (10) * Write a Boolean expression that evaluates to True if n is divisible by 3 and not by 7 (n % 3 == 0) and (n % 7 != 0) * Write a Boolean expression that evaluates to True if n is an odd two digit number (n >= 10) and (n <= 99) and (n % 2 != 0) * Consider the following Boolean expression for what value of n is it True and for what value of n is it False? ((n % 100 != 0) and (n % 4 == 0)) or (n % 400 == 0) True: n = 2000 False: n = 2009 # Write a program that prompts the user to enter the time in seconds # and writes out the number of hours, minutes, and seconds def main(): # Prompt the user to enter time in seconds time = input ("Enter time in seconds: ") # Time less than 1 minute if (time < 60): print "Time is:", time, "second" # Time is between 1 minute and 1 hour if ((time >= 60) and (time < 3600)): min = time / 60 sec = time % 60 print "Time is:", min, "minute", sec, "second" # Time is greater than or equal to an hour if (time >= 3600): hour = time / 3600 sec = time % 3600 min = sec / 60 sec = sec % 60 print "Time is:", hour, "hour", min, "minute", sec, "second" main() # Prompt the user to enter positive numbers. The user ends input by # typing a negative number. Write out the count of the numbers entered, # their sum and average def main(): # Intialize count and sum count = 0 sum = 0 # Prompt the user to enter a number num = input ("Enter a number: ") # Prompt repeatedly for input while (num >= 0): count = count + 1 sum = sum + num num = input ("Enter a number: ") # Write the count, sum, and average if (count == 0): print "Count = ", count else: avg = float (sum) / count print "Count = ", count, " Sum = ", sum, " Average = ", avg main() # Prompt the user to enter a number and then print the reverse of it def main(): # Prompt the user to enter a number num = input ("Enter a number: ") while (num < 0): num = input ("Enter a number: ") revNum = 0 while (num > 0): revNum = (revNum * 10) + (num % 10) num = num / 10 print "Reverse number: ", revNum main()