Algorithm Exercise List – Conditional Structures
1) Write a program that calculates the area of a circle with radius r, testing whether the
radius value is less than zero.
2) Write a program to check if a given integer is divisible by 3 or 5, but not
simultaneously by both.
3) Using the switch command, write a program that reads an integer between 1 and 7
and prints the day of the week corresponding to that number. That is, Sunday if 1, Monday
if 2, and so on.
4) The “APRENDER” school pays its teachers by the hour/class. Write an algorithm that
calculates and displays a teacher's salary. It is known that the value of the hour/class follows
the table below:
• Level 1 Teacher R$12.00 per hour/class Teacher
• Level 2 Teacher R$17.00 per hour/class
• Level 3 Teacher R$25.00 per hour/class
5) Write a program that reads the coefficients of a quadratic equation. Then,
calculate and show the roots of this equation, remembering that the roots are calculated
as:
where ∆ = b2 – 4 * a * c and ax2 + bx + c = 0 represents a quadratic equation. The variable
a must be different from zero. If it is equal, print the message “Not a quadratic equation”.
Otherwise, print:
• If ∆ < 0, there is no real number. Print the message “No root”.
• If ∆ = 0, there is one real root. Print the root and the message “Single root”.
• If ∆ > 0, there are two real roots. Print the roots.
6) Write a program that reads a worker's salary and the amount of a loan payment. If
the payment:
• Is greater than 20% of the salary, print: “Loan not granted.”
• Otherwise, print: “Loan granted.”
7) Suppose a cashier only has bills of 1, 10 and 100 reais. This algorithm is designed to
calculate the minimum number of each bill that the cashier should provide as change, after
a purchase is made. The algorithm will receive as input the purchase amount and the
payment amount, both in whole numbers. If the payment amount is less than the purchase
amount, the calculation will not be performed and the following message should be
displayed: "Payment Denied".
For example:
- Purchase amount: R$ 725
- Payment amount: R$ 1000
- Change: R$ 275
In this case, the result should show:
- 2 bills of R$ 100
- 7 bills of R$ 10
- 5 bills of R$ 1
8) The BMI (body mass index) is a WHO (World Health Organization) criterion that
provides an indication of the body weight of an adult person. The BMI formula is calculated
by dividing weight by height squared. Write an algorithm to provide the weight and height
of an adult, and thus show their condition according to the following table:
BMI in adults:
• Below 18.5: Underweight
• Between 18.5 and 25: Normal weight
• Between 25 and 30: Overweight
• Over 30: Obese
9) Write an algorithm that reads the ages of 2 men and 2 women (consider that the
ages of the men will always be different, as well as those of the women). Calculate and
write the sum of the ages of the older man with the younger woman, and the product of
the ages of the younger man with the older woman.
10) Write an algorithm to calculate the amount to be paid for a product, considering the
original price and the chosen payment method. Consult the table below to identify the
chosen payment code and perform the corresponding calculation
Code Payment condition
1 Pay in cash or check, get 10% discount
2 Pay in cash by credit card, get 15% discount
3 Pay in two installments, regular sticker price without interest
4 Pay in two installments, regular sticker price plus 10% interest
11) Write a program that reads the registration number, the 3 grades obtained by the
student in the 3 assessments and the average of the exercises that make up the assessment,
to then calculate the average score, following the formula:
MT = (grade 1 + grade 2 + grade 3 + ME) / 4
The algorithm should write the student's registration number, their grades, the average of
the exercises, the total average and the corresponding grade and the message 'Approved'
if the grade is A, B or C, and 'Failed' if the grade is D or E.
Average score Grade
>= 90 A
>= 75 and < 90 B >= 60 and < 75 C
>= 40 e < 60 D
< 40 E
12) Develop a program that displays a menu with the four basic operations (addition,
subtraction, multiplication and division). The user must choose which operation he/she
wants to perform. After this choice, ask for two numerical values and perform the
operation. At the end, show the result of the operation.
13) Write a program that asks for the 3 sides of a triangle. The program must
inform if the values can be a triangle. Indicate, if the sides form a triangle, if it is:
equilateral, isosceles or scalene.
Tips: Three sides form a triangle when the sum of any two sides is greater than the
third;
• Equilateral triangle: three equal sides;
• Isosceles triangle: any two equal sides;
• Scalene triangle: three different sides;
14) Read a date and determine if it is valid. In other words, check if the month is
between 1 and 12, and if the day exists in that month. Note that February has 29 days in
leap years, and 28 days in non-leap years.
15) The rates for a certain parking lot are as follows:
• 1st and 2nd hour - R$ 1.00 each
• 3rd and 4th hour - R$ 1.40 each
• 5th hour and beyond - R$ 2.00 each
The number of hours to be paid is always an integer and rounded up. Thus, someone
who parks for 61 minutes will pay for two hours, which is the same as they would pay
if they had stayed for 120 minutes. The times of arrival at the parking lot and departure
from it are presented in the form of pairs of integers, representing hours and minutes.
For example, the pair 12 50 will represent "ten to one in the afternoon". The aim is to
create a program that, after reading the arrival and departure times from the keyboard,
writes the price charged for parking on the screen. It is assumed that arrival and
departure occur no more than 24 hours apart. Therefore, if a given arrival time is later
than the departure time, this is not an error situation, but rather means that the
departure occurred the day after the arrival.
16) Write an algorithm that reads a value in reais and calculates the smallest possible
number of notes of 100, 50, 10, 5 and 1 into which the value read can be broken down.
Write the value read and the list of notes required.
17) A company decides to give its employees a raise according to a table that considers
the current salary and the length of service of each employee. Employees with lower
salaries will receive a proportionally larger salary increase than employees with higher
salaries, and depending on their length of service at the company, each employee will
receive a salary bonus. Write a program that reads:
• the employee's current salary;
• the employee's length of service at the company (number of years of work at the
company). Use the tables below to calculate the adjusted salary for this employee and print
the final adjusted salary value, or a message if the employee is not entitled to any increase.
Current Adjustment Service Bonus
Salary Time
Until 25% No Bonus
500,00
Until 20% Of 1 the 3 100,00
1000,00 years
Until 15% Of 4 the 6 200,00
1500,00 years
2000,00 10% Of 7 the 300,00
10 years
Above No More of 500,00
2000 Adjustment 10 years
18) Given three values A, B and C, build an algorithm that prints the values in descending
order (from largest to smallest).