Type C- Questions and Answers:
Question 1
Write a program to print one of the words negative, zero, or positive,
according to whether variable x is less than zero, zero, or greater than zero,
respectively.
Solution
x = int(input("Enter x: "))
if x < 0:
print("negative")
elif x > 0:
print("positive")
else:
print("zero")
Output
Enter x: -5
negative
Enter x: 0
zero
Enter x: 5
positive
Question 2
Write a program that returns True if the input number is an even number,
False otherwise.
Solution
x = int(input("Enter a number: "))
if x % 2 == 0:
print("True")
else:
print("False")
Output
Enter a number: 10
True
Enter a number: 5
False
Question 3
Write a Python program that calculates and prints the number of seconds in
a year.
Solution
days = 365
hours = 24
mins = 60
secs = 60
secsInYear = days * hours * mins * secs
print("Number of seconds in a year =", secsInYear)
Output
Number of seconds in a year = 31536000
Question 4
Write a Python program that accepts two integers from the user and prints a
message saying if first number is divisible by second number or if it is not.
Solution
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if a % b == 0:
print(a, "is divisible by", b)
else:
print(a, "is not divisible by", b)
Output
Enter first number: 15
Enter second number: 5
15 is divisible by 5
Enter first number: 13
Enter second number: 7
13 is not divisible by 7
Question 5
Write a program that asks the user the day number in a year in the range 2
to 365 and asks the first day of the year — Sunday or Monday or Tuesday
etc. Then the program should display the day on the day-number that has
been input.
Solution
dayNames = ["MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY",
"SATURDAY", "SUNDAY"]
dayNum = int(input("Enter day number: "))
firstDay = input("First day of year: ")
if dayNum < 2 or dayNum > 365:
print("Invalid Input")
else:
startDayIdx = dayNames.index(str.upper(firstDay))
currDayIdx = dayNum % 7 + startDayIdx - 1
if currDayIdx >= 7:
currDayIdx = currDayIdx - 7
print("Day on day number", dayNum, ":", dayNames[currDayIdx])
Output
Enter day number: 243
First day of year: FRIDAY
Day on day number 243 : TUESDAY
Question 6
One foot equals 12 inches. Write a function that accepts a length written in
feet as an argument and returns this length written in inches. Write a second
function that asks the user for a number of feet and returns this value. Write
a third function that accepts a number of inches and displays this to the
screen. Use these three functions to write a program that asks the user for a
number of feet and tells them the corresponding number of inches.
Solution
def feetToInches(lenFeet):
lenInch = lenFeet * 12
return lenInch
def getInput():
len = int(input("Enter length in feet: "))
return len
def displayLength(l):
print("Length in inches =", l)
ipLen = getInput()
inchLen = feetToInches(ipLen)
displayLength(inchLen)
Output
Enter length in feet: 15
Length in inches = 180
Question 7
Write a program that reads an integer N from the keyboard computes and
displays the sum of the numbers from N to (2 * N) if N is nonnegative. If N
is a negative number, then it's the sum of the numbers from (2 * N) to N.
The starting and ending points are included in the sum.
Solution
n = int(input("Enter N: "))
sum = 0
if n < 0:
for i in range(2 * n, n + 1):
sum += i
else:
for i in range(n, 2 * n + 1):
sum += i
print("Sum =", sum)
Output
Enter N: 5
Sum = 45
Enter N: -5
Sum = -45
Question 8
Write a program that reads a date as an integer in the format
MMDDYYYY. The program will call a function that prints print out the
date in the format <Month Name> <day>, <year>.
Sample run :
Enter date : 12252019
December 25, 2019
Solution
months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"]
dateStr = input("Enter date in MMDDYYYY format: ")
monthIndex = int(dateStr[:2]) - 1
month = months[monthIndex]
day = dateStr[2:4]
year = dateStr[4:]
newDateStr = month + ' ' + day + ', ' + year
print(newDateStr)
Output
Enter date in MMDDYYYY format: 12252019
December 25, 2019
Question 9
Write a program that prints a table on two columns — table that helps
converting miles into kilometers.
Solution
print('Miles | Kilometres')
print(1, "\t", 1.60934)
for i in range(10, 101, 10):
print(i, "\t", i * 1.60934)
Output
Miles | Kilometres
1 1.60934
10 16.0934
20 32.1868
30 48.2802
40 64.3736
50 80.467
60 96.5604
70 112.6538
80 128.7472
90 144.8406
100 160.934
Question 10
Write another program printing a table with two columns that helps convert
pounds in kilograms.
Solution
print('Pounds | Kilograms')
print(1, "\t", 0.4535)
for i in range(10, 101, 10):
print(i, "\t", i * 0.4535)
Output
Pounds | Kilograms
1 0.4535
10 4.535
20 9.07
30 13.605
40 18.14
50 22.675
60 27.21
70 31.745
80 36.28
90 40.815
100 45.35
Question 11
Write a program that reads two times in military format (0900, 1730) and
prints the number of hours and minutes between the two times.
A sample run is being given below :
Please enter the first time : 0900
Please enter the second time : 1730
8 hours 30 minutes
Solution
ft = input("Please enter the first time : ")
st = input("Please enter the second time : ")
# Converts both times to minutes
fMins = int(ft[:2]) * 60 + int(ft[2:])
sMins = int(st[:2]) * 60 + int(st[2:])
# Subtract the minutes, this will give
# the time duration between the two times
diff = sMins - fMins;
# Convert the difference to hours & mins
hrs = diff // 60
mins = diff % 60
print(hrs, "hours", mins, "minutes")
Output
Please enter the first time : 0900
Please enter the second time : 1730
8 hours 30 minutes
Please enter the first time : 0915
Please enter the second time : 1005
0 hours 50 minutes