MILLAT UMIDI UNIVERSITY
COURSE OF COMPUTER SCIENCE
LABORATORY PRACTICE n. 2
Exercise 1:
Write down a Python program in order to:
• Read a positive integer number n.
• Print out a proper message according to the fact that it is an odd or an even value.
Exercise 2:
Write down a Python program which:
• Reads three integer numbers from the keyboard.
• Displays an appropriate message on screen according to the fact that:
o All the three numbers assume the same value
o Two (and only two) numbers have the same value
o All the three numbers assume a different value
Example: the following is a possible program execution (underlined text is typed by the user).
Introduce the first value: 46
Introduce the second value: 21
Introduce the third value: 46
Only two numbers have the same value.
Exercise 3:
Write down a Python program which reads three real numbers from the keyboard and displays
the maximum among them, also saying if it corresponds to the first, second or third value
introduced by the user.
Example: the following is a possible program execution (underlined text is typed by the user).
Introduce the first value: 3.5
Introduce the second value: 12.1
Introduce the third value: -1.0
The second value (12.1) is the largest one.
Exercise 4:
Write a Python program working as a simple prefix calculator: the program must read a prefix
2expression, consisting of an operator (+, -, *, /) followed by two real numeric operands, then
display the operation in the standard format and the result of the expression itself.
Example: the following is a possible program execution (underlined text is typed by the user).
Input expression: + 3.5 5.2
Operation result: 3.5 + 5.2 = 8.7
Suggestion: read the operator as a character, then check it in order to understand the operation to
be applied to the operands.
Exercise 5:
Write down a Python program which:
• Reads three real numbers (a, b and c)
• Checks whether a, b and c may (or may not) represent the sides’ lengths of a triangle. If
yes, the program must also determine the triangle type, distinguishing among the
following cases:
o Equilateral triangle, a==b==c
o Isosceles triangle, a==b!=c or b==c!=a or a==c!=a
o Scalene triangle a!=b!=c
o Rectangular triangle c*c==a*a+b*b or a*a==b*b+c*c or b*b==a*a+c*c
Recall that, in any triangle, the length of each side must be smaller than the sum of the lengths of
the other two sides.
a<b+c, b<a+c, c<a+b
Exercise 6:
Write down a Python program in order to:
• Read a character.
• Print out a proper message according to the fact that it is vowel or consonant.
Exercise 7:
Write down a Python program which:
• Reads year from the keyboard.
• Displays an appropriate message on screen according to the fact that year is leap year or
not:
Tip: Leap year
All years which are perfectly divisible by 4 are leap years except for century years( years ending
with 00 ) which is a leap year only it is perfectly divisible by 400. For example: 2012, 2004,
1968 etc are leap year but, 1971, 2006 etc are not leap year. Similarly, 1200, 1600, 2000, 2400
are leap years but, 1700, 1800, 1900 etc are not.
Exercise 8:
Write down a Python program which reads three real numbers from the keyboard and displays
the roots of a quadratic equation ax2+bx+c=0 where a, b and c are coefficients. This program will
ask the coefficients: a, b and c from user and displays the roots.