DW-3 Conditional Structures
DW-3 Conditional Structures
Exercise 1 : (Course)
Write the algorithm that determines the maximum between two integers using the two
forms of the if...else statement.
Exercise 2 : (Course)
Write an algorithm that reads the temperature of the water then displays the state of
the water (steam, liquid, or ice).
.
Exercise 3 : (Course)
Write an algorithm that calculates the square root of a given integer, as well as its
absolute value, then indicates whether it is even or odd.
Exercise 4 : (Course)
• Write an algorithm that converts a value of the metric system given in value of the
American measurement system.
• The user has the choice to provide a measure to your algorithm in meters, grams or
Celsius degrees and you should convert it to feet, pounds or degrees Fahrenheit.
(Remember to use the instruction “Case of")
• Here are the conversion rules to use :
1 foot = 0.3048 meters
1 gram = 0.002205 pounds
Fahrenheit degree temperature = 32 + (1.8*temperature in degrees Celsius).
• Example : 12.3 m = 40.35 feet
Exercise 5 : (DW)
Write an algorithm that allows you to ask the user for two real numbers, and then
inform him if their product is negative, positive, or zero without calculating the
product between the two numbers.
Exercise 6 : (DW)
Write an algorithm that asks for the time in the form of 3 numbers (in hours H,
minutes M, and seconds S), then check if the time is valid, then say what the time will
be after 30 seconds in the valid case.
4|Page
Université Saad Dahlab 1ière Année / MI
Faculté des Sciences Algo1
Exercise 7 : (DW)
Consider the following part of the algorithm :
Read (A, B, C);
If (A>0 and B<=10) then
CC-1;
If (B>0 or C<>5) then
BB-5; AA-2;
Else
CC*2;
EndIf;
Else
If (C>10 or B>3) then
CB-3*C;
EndIf;
BB-C;
EndIf;
If(A=1 or B=0 or C=1) then
CA+B;
EndIf;
BC+A*B;
- Complete the following table which represents an execution trace (progress) of the
previous algorithm following 4 cases (the first case is given as an example) :
Case Instructions to execute A B C
Read (A, B, C) 3 5 1
CC-1; 3 5 0
BB-5 ; 3 0 0
a) A=3, B=5, C=1
AA-2; 1 0 0
CA+B; 1 0 1
BC+A*B; 1 1 1
b) A=2, B=0, C=6
c) A=0, B=5, C=1
d) A=0, B=1, C=1
Exercise 8 : (DW)
Write an algorithm which: reads 2 integers, reads an arithmetic operator among (-, +,
/, *), applies the operator read to the two numbers read and displays the result taking
into account the cases of errors (Example : division by 0, invalid operator…).
Exercise 9 : (Facultatif)
In a company, the basic salary of an employee is 30,000 da. A bonus is granted to
employees according to the following criteria:
- If his age is > 45 years old, then the bonus is 6000 da
- If he is a team leader and his seniority is > 3 years, then the bonus is 10,000 da.
- If his seniority is > 10 years, then the bonus is 5000 da
Write an algorithm that reads for an employee his age, his seniority (in number of
years), and whether he is a team leader or not, then displays his salary knowing that:
Employee salary = base salary + bonus.
Please note: an employee can only have one bonus (the largest possible).
5|Page