0% found this document useful (0 votes)
51 views2 pages

Conditional Statements and Algorithms Exercises

Exercises

Uploaded by

chifaachott
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views2 pages

Conditional Statements and Algorithms Exercises

Exercises

Uploaded by

chifaachott
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

University of Djilali BOUNAAMA Khemis Miliana Algorithms and Data Structures I

Faculty of Sciences and Technology 1st year MI


Department of Mathematics and Computer Science AZZOUZA N. HANICHE F.

SW N°2 : CONDITIONL / SELECTION STATEMENT

Exercise 01 :

Answer with “true” or “false” to the following instructions and correct any errors:

1) if N := S ; then 2) if A > B 3) if N1 = 0, N2 = 0 then


...... ........ ......
endif endif endif

4) if i := i +1 ; then 5) if A:=10 et B := 20 then 6) if mois est 'mars' then


... ... ...
endif endif endif

7) if nbr est Positif then 8) if A <> B et C then 9) if [N >0] et [N < 10] then
... ... ...
endif endif endif

10) if X > Y then 11) if X >= 0 then


... ....
else then if X = 0 then
... endif ...
endif endif...

Exercise 02 : Write the algorithm that finds and displays the maximum and minimum of three entered
integer values.

Exercise 03 : Write an algorithm that calculates and displays the absolute value of any integer read from
keyboard.

Exercise 04 : Write the algorithm which allows you to enter a number then determine if it belongs to a
given interval, knowing that the ends(limits) of the interval are fixed by the user.

Exercise 05 : Write the algorithm that allows you to enter the day, month and year of a date (the month
given as a number), to determine if it is a valid date and then display it by transforming the number of the
month into its name (for example March 3, 2012)

Note: use the “case / switch” statement for the last part.

Exercise 06 (Exam 2022-2023): Given a date read from the keyboard as a 5 or 6 digit integer (see
examples) representing a date from the 21st century. Write an algorithm that decodes the given date and
displays the number of the day, month and year on 4 digits.
Examples : date = 270622 ------> Day number : 27 | date = 51109 ------> Day number : 5
Month number : 6 | Month number : 11
Year : 2022 | Year : 2009

1
Exercise 07 : (Exam 2013/2014) : Write an algorithm that asks for a date in the form of 3 integers (day,
month number and year) and displays the season (ex: 02/12/2008; winter of 2008). (We will assume that
the first day of the season is always the 21st)

Exercise 08 : Propose two variations an algorithm that asks the user for a child’s age. Then, inform the
user about the child’s category as follows:
1. "Poussin" from 6 to 7 years old.
2. "Pupille" from 8 to 9 years old.
3. "Minime" from 10 to 11 years old.
4. "Cadet" for ages 12 and older.

Exercise 09 : (Exam 2017/2018) : In a company, the calculation of paid leave days is carried out as
follows: if a person has joined the company for less than a year, they are entitled to two days of leave per
month (at least 1 month), otherwise at least 28 days. If this person is an executive, if he is at least 35 years
old, and if his seniority is greater than 3 years, he is granted 2 additional days. If he is an executive and if
he is at least 45 years old and if his seniority is greater than 5 years, he is granted 4 additional days, in
addition to the 2 granted for over 35 years.

1. Write an algorithm that calculates the number of days of leave from age expressed in years, seniority
expressed in months and membership in executives (being an executive or not).

Exercise 10 : A year is “leap” (that contains 366 days) if it is a multiple of 4, except the years at the start
of the century (which end in 00) which are not leap years unless they are divisible by 400.
Example: - 1980 and 1996 are leap years because they are divisible by 4.
- 2000 is a leap year because it is divisible by 400.
- 2100 and 3000 are not leap years because they are not divisible by 400.
Write an algorithm that allows you to determine whether a given positive integer is a leap year or not.

Exercise 11 : A bank is open from 8:00 to 12:00 and from 14:00 to 17:00 except Thursday afternoon and
Friday all day.
1. Write an algorithm that asks the user to enter a day (weekday) and time (in hours) and displays the
message "Your bank is open" if the bank is open, and "Sorry, your bank is closed" otherwise.
2. Update the previous algorithm to indicates when and at what time the user can return if the bank is
closed.

Common questions

Powered by AI

The algorithm checks the current day and time against the bank's schedule: open from 8:00 to 12:00 and 14:00 to 17:00, excluding Thursday afternoons and all of Friday. It uses a series of if-else statements to compare input time against this schedule and returns messages based on these comparisons. If closed, it generates alternative open times by determining the next valid time slot when the bank is open, helping guide users on when to return .

The algorithm uses age thresholds to classify a child's category. Ages 6-7 are classified as "Poussin," 8-9 as "Pupille," 10-11 as "Minime," and 12 and older as "Cadet." This categorization is implemented through a series of conditional statements, likely utilizing if-else constructs or switch cases to match specific age ranges to their respective categories and output the correct classification .

Error handling strategies include validating inputs through condition checks before processing, utilizing try-catch blocks where applicable, prompting user re-entry upon invalid input, and providing clear feedback on acceptable input formats. Additionally, implementing input sanitization, such as trimming whitespace and checking for required input types before conversion operations, enhances robustness against common input errors .

To determine the season, the algorithm assumes each season starts on the 21st of a specific month: March 21 for spring, June 21 for summer, September 21 for autumn, and December 21 for winter. It compares the entered day and month against these dates to assign the current season, adjusting for day inclusivity based on the specific start date of each season .

The algorithm involves initially assuming one of the three numbers as minimum and maximum, then iteratively comparing each number against these assumptions. It updates the minimum if any number is smaller than the current minimum, and similarly updates the maximum if any number is larger than the current maximum. These conditional comparisons are facilitated by if-else statements that ensure every number is checked against the others to derive both maximum and minimum values accurately .

The algorithm first checks if the year is divisible by 4. If true, it then checks if it is a century year (ending in 00); if so, it must also be divisible by 400 to be a leap year. This three-step check ensures that most years divisible by 4 but not century years are leap years, while century years are only leap years if divisible by 400. Example checks include years like 1980 and 2000, while disqualifying 2100 and 3000 .

The algorithm first requires input of the interval bounds from the user, followed by the target number. It then checks if the target number is greater than or equal to the lower bound and less than or equal to the upper bound using conditional statements. This ensures the number is within the closed interval defined by the user, accounting for boundary inclusivity .

Common errors include improper use of operators like '**et**' without ensuring proper syntax for both conditions, or ambiguous conditions such as 'N1 = 0, N2 = 0' without a clear conjunction or disjunction. Corrective actions involve ensuring each condition has explicit AND (&&) or OR (||) operators to clearly define relationships between conditions, thereby preventing logic faults and compilation errors .

The conditions evaluated for additional paid leave days include: being an executive, being at least 35 years old and having more than 3 years of seniority for 2 additional days, and being at least 45 years old with more than 5 years of seniority for a total of 4 additional days. The algorithm evaluates these conditions using conditional statements, likely nested if-else conditions, to check each criterion sequentially and accumulate additional leave days accordingly .

The algorithm requires the user to enter the day, month, and year of a date with the month given as a number. It checks validity by ensuring the day is within the valid range for the given month and adjusting for leap years if necessary. To transform the month's number into its name, it uses a 'case/switch' statement, mapping integer month values to their respective string names, such as 3 to March .

You might also like