0% found this document useful (0 votes)
2 views2 pages

Lab 2

The document outlines a lab exercise focused on using if statements in programming. It includes two questions: the first requires writing a program to calculate the time difference between two military format times, while the second (optional) asks for a program that determines the number of days in a given month. Various scenarios are provided to test the first program's functionality.

Uploaded by

singh.angad0905
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Lab 2

The document outlines a lab exercise focused on using if statements in programming. It includes two questions: the first requires writing a program to calculate the time difference between two military format times, while the second (optional) asks for a program that determines the number of days in a given month. Various scenarios are provided to test the first program's functionality.

Uploaded by

singh.angad0905
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Lab Exercise 2

Objective: practice using if statement

Question 1:

Write a program that reads two times in military format (0900, 1730) and prints the number of hours
and minutes between the two times. Assume the time difference is within 24 hours. Here is a sample
run. User inputs are in red color and the output is in green color.

Please enter the first time: 0900

Please enter the second time: 1730

Output:

The difference is 8 hours and 30 minutes.

Hints: Use input() statement to get an input from user, for example

firstTime=input(“Please enter the first time”)

To get the hours: hours=int(firstTime[0:2])

To get the minutes: minutes = int(firstTime[2:])

Please note the if statements should deal with scenarios other than the example shown above:

Scenario 1: the hours in second time is greater than hours in the first time, but minutes is less. For
example

Please enter the first time: 0930

Please enter the second time: 1700

Output:

The difference is 7 hours and 30 minutes.

Scenario 2: the hours in second time is less than hours in the first time, but minutes is greater. For
example

Please enter the first time: 1700

Please enter the second time: 0930

Output:

The difference is 16 hours 30 minutes.

Scenario 3: the hours in second time is less than hours in the first time, but minutes is also less. For
example

Please enter the first time: 1730


Please enter the second time: 0900

Output:

The difference is 15 hours 30 minutes.

Please test run your program with all the scenarios and upload the file on Moodle.

Question 2 (Optional):

Write a program that asks the user to enter a month (1 for January 2 for February, and so on) and then
prints the number of days in the month. For February, print “28 or 29 days”.

Sample run:

Enter a month: 5
5 is May and it has 30 days

You might also like