You are on page 1of 6

Department of Computing

CS114: Fundamentals of Programming


Class: BESE 9 AB

Lab 05: Decision Making using Condition Statements


CLO1: Identify the syntax and semantics of different programming constructs

Date: October 5th 2018


Time: 9:00am -12:00pm and 02:00pm -05:00pm
Instructor: Ms. Hania Aslam
Lab 05: Decision Making using Condition Statements

Introduction

If/else statement is a basic decision making statement in programming. It executes one block of
statements when the condition is true and other when it is false. In any situation, one block is
executed and other is skipped.

Objectives
The objective of this lab is to understand if/else statement

Tools/Software Requirement
Python IDLE

Description

Basic relational operators used with if else statement

> greater than 5 > 4 is TRUE

< less than 4 < 5 is TRUE

>= greater than or equal 4 >= 4 is TRUE

<= less than or equal 3 <= 4 is TRUE

== equal to 5 == 5 is TRUE

!= not equal to 5 != 4 is TRUE

Syntax:

if(Condition):

Statement

else:

Statement
Lab Tasks:
Using only the programming techniques that you have learned so far, perform the
following tasks:

Note: All the tasks of this lab should be performed in Python scripted mode only.

1. Write a program that reads an integer and determines and prints whether it is odd or even.

[1
Mark]
Task 1 Snap
#Add here the snap showing your Python code and the required output.

2. Write a program that inputs three different integers from the keyboard, and then prints the
smallest and the largest of these numbers. Use only the single-selection form of the if
statement that you learned in the class. The screen dialogue should appear as follows:
Hint: You can use split() function in combination with input() function to take multiple
input values from user in a single line.

Input three different integers: 13 27 14


Smallest is 13
Largest is 27

[1
Mark]
Task 2 Snap
#Add here the snap showing your Python code and the required output.

3. Modify above program to read in five integers and then determine and print the largest
and the smallest integers in the group.

[1
Mark]
Task 3 Snap
#Add here the snap showing your Python code and the required output.

4. Write a program that reads three nonzero integers and determines and prints if they could
be the sides of a right triangle.

Enter three integers: 3 4 5


The three integers are the sides of a right triangle

Enter three integers: 9 4 1


The three integers are not the sides of a right triangle

Hint: Pythagoras Theorem states that in a right angled triangle, the square of the


hypotenuse is equal to the sum of the squares of the other two sides.

[1
Mark]
Task 4 Snap
#Add here the snap showing your Python code and the required output.

5. A palindrome is a number or a text phrase that reads the same backwards as forwards.
For example, each of the following five-digit integers are palindromes: 12321, 55555,
45554 and 11611. Write a program that reads in a five-digit integer and determines
whether or not it is a palindrome.
Help resource: String slicing , indexing and striding

Enter a five-digit number: 18181


18181 is a palindrome

Enter a five-digit number: 16738


16738 is not a palindrome
https://www.eecs.wsu.edu/~cs150/tutorial/selection/soln3_35.html

[2
Marks]
Task 5 Snap
#Add here the snap showing your Python code and the required output.

6. Write a program that takes as input a letter and displays if it is a vowel using conditional
statements. [2 Marks]
Task 6 Snap
#Add here the snap showing your Python code and the required output.

7. Write a program to calculate energy bill using conditional statements. Read the starting and
ending meter reading. The charges are as follows:
[2 Marks]

No. of Units Charges


>600 5.50
300-600 4.50
200-300 3.50
0-200 1.50

Task 7 Snap
#Add here the snap showing your Python code and the required output.

Deliverables
Compile a single Word document by filling in the solution/answer part (as directed) along with
the snapshots. Name your submission file as given below and submit this Word file on LMS
before the deadline.

Name – Registration No. – Section

Grade Criteria
This lab is graded. Min marks: 0. Max marks: 10.
Activity Minimum Maximum
Documentation with clearly defined Fail Pass
understanding of the lab task and approach
Task 1 0 01
Task 2 0 01
Task 3 0 01
Task 4 0 01
Task 5 0 02
Task 6 0 02
Task 7 0 02

You might also like