0% found this document useful (0 votes)
10 views5 pages

Algorithm Exercises on Conditional Structures

The document contains a list of algorithm exercises focused on conditional structures, covering various programming tasks such as calculating areas, checking divisibility, determining salaries, and validating dates. Each exercise presents a specific problem that requires the use of conditional statements and arithmetic operations to arrive at a solution. The exercises range from basic calculations to more complex algorithms involving user input and decision-making.
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)
10 views5 pages

Algorithm Exercises on Conditional Structures

The document contains a list of algorithm exercises focused on conditional structures, covering various programming tasks such as calculating areas, checking divisibility, determining salaries, and validating dates. Each exercise presents a specific problem that requires the use of conditional statements and arithmetic operations to arrive at a solution. The exercises range from basic calculations to more complex algorithms involving user input and decision-making.
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

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).

Common questions

Powered by AI

Start by computing the total parking duration in whole hours, rounding up partial hours. The first two hours charge R$1.00 each, the next two hours R$1.40 each, and subsequent hours R$2.00 each. This tiered billing algorithm ensures charges align with defined temporal rules, accommodating any duration up to 24 hours .

The Body Mass Index (BMI) is calculated using the formula: BMI = weight (kg) / (height (m))^2. Once calculated, categorize the BMI as follows: Below 18.5 as Underweight, Between 18.5 and 25 as Normal weight, Between 25 and 30 as Overweight, and Over 30 as Obese. This classification helps in assessing an adult's body weight status .

For a quadratic equation ax^2 + bx + c = 0, where a ≠ 0, calculate the discriminant Δ = b^2 - 4ac. If Δ < 0, no real roots exist; if Δ = 0, one real root exists and should be output as "Single root." If Δ > 0, calculate the two real roots using the quadratic formula and print them. This method assesses the equation's structure to determine root characteristics .

The algorithm should use a greedy approach where it first determines the total change required. Then, it calculates the maximum number of the largest denomination notes (e.g., R$100), followed by R$50, R$10, R$5, and finally R$1 until the total is achieved. Subtract the equivalent of the notes used from the total change iteratively until none remains .

An algorithm should calculate 20% of the provided salary, and compare this value to the loan payment amount. If the loan payment exceeds 20% of the salary, the output should be "Loan not granted." Otherwise, the output should be "Loan granted." This comparison ensures that the loan is aligned with financial guidelines for affordability .

An algorithm to determine if an integer is divisible by 3 or 5, but not both, should use conditional statements to evaluate the integer. First, check if the integer is divisible by 3 and not by 5 by verifying if the modulo operation 'number % 3 == 0' and 'number % 5 != 0'. Second, check if the integer is divisible by 5 and not by 3 with 'number % 5 == 0' and 'number % 3 != 0'. The algorithm logically combines these checks using the OR operator to satisfy the condition .

To validate a date, ensure the month is between 1 and 12. Assess day constraints by month, with February having 29 days in leap years and 28 in others. Use a formula to check if the year is a leap year: it must be divisible by 4, except centuries (divisible by 100) unless divisible by 400. This ensures date components align with calendrical rules .

To sort three numbers, compare each pair and rearrange the numbers in descending order. Use conditional checks to swap values as necessary, ensuring the largest value is first, followed by the next largest, until the smallest. This comparative strategy efficiently orders the numbers as required .

When calculating a teacher's salary, the program should first determine the teacher's level (1, 2, or 3) corresponding to hourly payment rates of R$12.00, R$17.00, or R$25.00, respectively. Multiply the hourly rate by the number of hours taught to determine the salary. This requires defining a table or a conditional structure to match the teacher's level to their hourly rate .

To verify if side lengths can form a triangle, the sum of any two sides must be greater than the third side. Once verified, classify the triangle as follows: Equilateral if all three sides are equal, Isosceles if exactly two sides are equal, and Scalene if all sides are different .

You might also like