Professional Documents
Culture Documents
My Python Lab Sheet2
My Python Lab Sheet2
LAB SHEET # 2
SUBMITTED TO
Title:
1. Number Analyzer
Write a program that asks the user to enter an integer. The program should display
“Positive” if the number is greater than 0, “Negative” if the number is less than 0, and
“Zero” if the number is equal to 0. The program should then display “Even” if the number is
even, and “Odd” if the number is odd.
Algorithm:
a. Start
b. Ask user to enter the number and save in a variable
c. If num is greater than zero show it is positive
d. If the num is zero show zero else show negative
e. If modulus 2 of the num is zero the num is even otherwise odd number
f. Print the result
g. Stop
Flowchart:
Start
read num
No No
num%2==0 num > 0
Yes
Display Odd Num
Yes No
num == 0
Display Even Num
Yes
Display + Num Display - num
End
Display Zero
End
Code:
num = float(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
if num%2==0:
print ("Even Number")
else:
Regn No.1442-320007 Muhammad Waqas Khalil MCS (Eve), Preston University Islamabad
[3]
Lab Sheet # 2 Programming Fundamentals
print("Odd Number")
Output (Compilation, Debugging & Testing):
Enter a number: 499996
Positive number
Even Number
***END***
Regn No.1442-320007 Muhammad Waqas Khalil MCS (Eve), Preston University Islamabad
[4]
Lab Sheet # 2 Programming Fundamentals
Title:
2. Areas of Rectangles
The area of a rectangle is the rectangle’s length times its width. Write a program that asks
for the length and width of two rectangles. The program should tell the user which rectangle
has the greater area, or if the areas are the same.
Algorithm:
a. Start
b. Read length and width of the 2 x rectangles
c. Calculate the are of both the rectangles
d. Using if statement check which rectangle is bigger, smaller or both are same
e. Display the result
f. Stop
Flowchart:
Start
No
area_1 > area_2
Yes No
area_2 > area_1
End
Regn No.1442-320007 Muhammad Waqas Khalil MCS (Eve), Preston University Islamabad
[5]
Lab Sheet # 2 Programming Fundamentals
Code:
length_1 = float(input('Enter length for triangle 1: '))
width_1 = float(input('Enter width for triangle 1: '))
area_1 = length_1 * width_1
length_2 = float(input('Enter length for triangle 2: '))
width_2 = float(input('Enter width for triangle 2: '))
area_2 = length_2 * width_2
if area_1 > area_2:
print('Rectangle 1 has the greatest area at', area_1)
elif area_2 > area_1:
print('Rectangle 2 has the greatest area at', area_2)
else:
print('Rectangles 1 and 2 have the same area')
Output (Compilation, Debugging & Testing):
Enter length for triangle 1: 15
Enter width for triangle 1: 20
Enter length for triangle 2: 14
Enter width for triangle 2: 19
Rectangle 1 has the greatest area at 300.0
***END***
Regn No.1442-320007 Muhammad Waqas Khalil MCS (Eve), Preston University Islamabad
[6]
Lab Sheet # 2 Programming Fundamentals
Title:
3. Quarter of the Year
Write a program that asks the user for a month as a number between 1 and 12. The
program should display a message indicating whether the month is in the first quarter, the
second quarter, the third quarter, or the fourth quarter of the year. Following are the
guidelines:
• If the user enters either 1, 2, or 3, the month is in the first quarter.
• If the user enters a number between 4 and 6, the month is in the second quarter.
• If the number is either 7, 8, or 9, the month is in the third quarter.
• If the month is between 10 and 12, the month is in the fourth quarter.
• If the number is not between 1 and 12, the program should display an error.
Algorithm:
a. Start
b. Ask the user to enter the month
c. Using if statement check the quarter of the month
d. Display the quarter of the month entered
e. Stop
Flowchart:
Start
read month
No
month < 1 or month > 12
No
Yes month >= 1 and month
Display Error
No
Yes month >= 4 and month <= 6
First Quarter
No
Yes month >= 7 and month <= 9
Second Quarter
Yes month >= 10 and month <= 12
Third Quarter
Yes
Fourth Quarter
End
Regn No.1442-320007 Muhammad Waqas Khalil MCS (Eve), Preston University Islamabad
[7]
Lab Sheet # 2 Programming Fundamentals
Code:
month = int(input('Enter a month between 1 and 12: '))
if month < 1 or month > 12:
message = "\nError. Month must be between 1 and 12.\n"
else:
if month >= 1 and month <= 3:
message = " First quarter"
elif month >= 4 and month <= 6:
message = " Second quarter"
elif month >= 7 and month <= 9:
message = " Third quarter"
elif month >= 10 and month <= 12:
message = " Fourth quarter"
print('Your Entered Month Num', month, 'lies in',message)
Output (Compilation, Debugging & Testing):
Enter a month between 1 and 12: 9
Your Entered Month Num 9 lies in Third quarter
***END***
Regn No.1442-320007 Muhammad Waqas Khalil MCS (Eve), Preston University Islamabad
[8]
Lab Sheet # 2 Programming Fundamentals
Title:
4. Mass and Weight
Scientists measure an object’s mass in kilograms and its weight in newtons. If you know the
amount of mass of an object in kilograms, you can calculate its weight in newtons with the
following formula: Weight = mass x 9.8
Write a program that asks the user to enter an object’s mass, then calculates its weight. If
the object weighs more than 500 newtons, display a message indicating that it is too heavy.
If the object weighs less than 100 newtons, display a message indicating that it is too light.
Algorithm:
a. Start
b. Ask the user to enter the mass of the object
c. Calculate the weight using the mass of the object
d. Using if statement check whether the object is heave, light or average
e. Display the result
f. Stop
Flowchart:
Start
read mass
No
weight < 100
Yes No
weight > 500
it is too heavy
Display Weight
End
Regn No.1442-320007 Muhammad Waqas Khalil MCS (Eve), Preston University Islamabad
[9]
Lab Sheet # 2 Programming Fundamentals
Code:
mass = int(input('Enter an object mass: '))
weight = mass * 9.8
if weight < 100:
message = ('it is too light')
elif weight > 500:
message = ('it is too heavy')
else:
message = ('it is average')
print('Weight is',weight,'so', message)
Output (Compilation, Debugging & Testing):
Enter an object mass: 20
Weight is 196.0 so It is average
***END***
Regn No.1442-320007 Muhammad Waqas Khalil MCS (Eve), Preston University Islamabad
[10]
Lab Sheet # 2 Programming Fundamentals
Title:
5. Roman Numerals
Write a program that prompts the user to enter a number within the range of 1 through 10.
The program should display the Roman numeral version of that number. If the number is
outside the range of 1 through 10, the program should display an error message. The
following table shows the Roman numerals for the numbers 1 through 10:
Algorithm:
a. Start
b. Ask the user to enter the number
c. Using if statement check the number
d. Display the corresponding roman value of the number
e. Stop
Regn No.1442-320007 Muhammad Waqas Khalil MCS (Eve), Preston University Islamabad
[11]
Lab Sheet # 2 Programming Fundamentals
Flowchart:
Start
read num
num == 1
Display Error
num == 2
I
num == 3
II
num == 4
III
num == 5
IV
num == 6
V
num == 7
VI
num == 8
VII
num == 9
VIII
num == 10
IX
End
Code:
num = int(input('Enter a number between 1 and 10: '))
if num < 1 or num > 10:
message = ("Wrong. Number must be between 1 and 10")
else:
if num == 1:
message = ("I")
elif num == 2:
message = ("II")
elif num == 3:
Regn No.1442-320007 Muhammad Waqas Khalil MCS (Eve), Preston University Islamabad
[12]
Lab Sheet # 2 Programming Fundamentals
message = ("III")
elif num == 4:
message = ("IV")
elif num == 5:
message = ("V")
elif num == 6:
message = ("VI")
elif num == 7:
message = ("VII")
elif num == 8:
message = ("VII")
elif num == 9:
message = ("IX")
elif num == 10:
message = ("X")
print('The number', num,'in Roman is', message)
Output (Compilation, Debugging & Testing):
Enter a number between 1 and 10: 12
The num 12 in Roman is Wrong. Number must be between 1 and 10
***END***
Regn No.1442-320007 Muhammad Waqas Khalil MCS (Eve), Preston University Islamabad