Programming Lab 3
Problem 1: Find output for each expression below:
Expression Output
a = 33 b is greater than a
b = 200
if b > a:
print("b is greater than a")
a = 200 a is greater than b
b = 33
if a > b:
print("a is greater than b")
a = 33 a and b are equal
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
a = 200 a is greater than b
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
a = 200 b is not greater than a
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
Problem 2:
Write a python program that asks the user to enter two integers a and b then print “Hello World”
message if a is greater than b.
Code:
a = int(input("Enter the first integer: "))
b = int(input("Enter the second integer: "))
if a > b:
print("Hello World")
Problem 3:
Write a python program that asks the user to enter two integers a and b then print “Hello World”
message if a is different to b.
Code:
a = int(input("Enter the first integer: "))
b = int(input("Enter the second integer: "))
if a != b:
print("Hello World")
Problem 4:
Write a python program that asks the user to enter two integers a and b then print “Both numbers
are equal” if a is equal to b, otherwise print “a and b are not equal”
Code:
a = int(input("Enter the first integer: "))
b = int(input("Enter the second integer: "))
if a == b:
print("Both numbers are equal")
else:
print("a and b are not equal")
Problem 5:
Write a python program that asks the user to enter two integers a and b then compare between
them to:
Print "1" if a is equal to b,
print "2" if a is greater than b,
otherwise print "3".
Code:
a = int(input("Enter the first integer: "))
b = int(input("Enter the second integer: "))
if a == b:
print("1")
elif a > b:
print("2")
else:
print("3")
Problem 6:
Write a python program that asks the user to enter three integers a, b, and c, then print “Hello
World” message if a is equal to b, and c is equal to d. Hint: use the “and” operator.
Code:
a = int(input("Enter the first integer: "))
b = int(input("Enter the second integer: "))
c = int(input("Enter the third integer: "))
if a == b and b == c:
print("Hello World")
Problem 7:
Write a python program that calculates the required tax based on the employer’s salary (Ask the
user to enter the salary in the beginning of the program). If the salary is less than 10,000 SAR
then the tax = 10% of the salary; otherwise, the tax = 15%.
Code:
salary = float(input("Enter your salary: "))
if salary < 10000:
tax = salary * 0.1
else:
tax = salary * 0.15
print("Your tax is:", tax, "SAR")
Problem 8:
Write a python program that asks the user to enter the mark of the student then to find and prints
the grade of student based on the mark. Hint: Use the if/elif/else Selection Structure.
The grades are:
95+: A+.
90-94 :A
85-89:B+
80-84:B
75-89:C+
70-74:C
65-69:D+.
60-64:D
<60: F
Code:
mark = int(input("Enter the mark of the student: "))
if mark >= 95:
grade = "A+"
elif mark >= 90:
grade = "A"
elif mark >= 85:
grade = "B+"
elif mark >= 80:
grade = "B"
elif mark >= 75:
grade = "C+"
elif mark >= 70:
grade = "C"
elif mark >= 65:
grade = "D+"
elif mark >= 60:
grade = "D"
else:
grade = "F"
print("The grade of the student is:", grade)