Programming Exercises for Algorithm Development
Programming Exercises for Algorithm Development
SEQUENCE
1. Write an algorithm that calculates double of a given input number.
2. Write an algorithm that, given three real numbers X, Y, and Z, calculates the result of (X + Y + Z)2.
3. Write an algorithm that, given the price in euros as input, calculates its value in lire.
(N.B. 1 € = 1936.27 £).
4. Write the algorithm that, given an input time through its three components (hours,
minutes and seconds), calculate the total value in seconds.
5. Write an algorithm that calculates the area of a trapezoid, given the measurements of the bases and the height.
6. Write the algorithm that, given two numbers a and b (where a < b), calculates the width of the interval (a, b)
and the product of the extremes.
7. Write the algorithm that, given two integer numbers x and y, calculates the result and the remainder of the division.
interaction between x and y.
8. Write an algorithm that, given the ages of three people, calculates the average age.
9. Write an algorithm that, given the price of a product, calculates the price discounted by 20%.
10. Write an algorithm that, given the coordinates of two points in the plane, calculates the distance between them.
11. Write the algorithm that, given two strings, displays them in sequence (without intermediate spaces) and
communicate the length of the resulting string (only for the Visual programming course)
Basic)
SELECTION
12. Write the algorithm that, given a numerical value as input, tells whether it is positive or negative.
13. Write the algorithm that, when reading an integer input, determines if it is even or odd.
14. Write an algorithm that, given two integer inputs, checks if the first is a multiple of the second.
second.
15. Write an algorithm that, given an integer input, displays its double if it is even, the
triple if it is odd.
16. Write the algorithm that, given two numbers, calculates the sum if they are both positive, the product
otherwise.
17. Write an algorithm that, given the price of a product, applies a 12% discount if the price
is less than € 30.00, 25% otherwise.
18. Write the algorithm that, given the base and height of a triangle, calculates the area if they are both
positive, or print the message "Incorrect input values".
19. Write the algorithm that, given the endpoints a and b of an interval and a value x, displays the
messaggio "Il valore è interno all'intervallo" sea≤x≤b, altrimenti "Il valore è esterno
all'intervallo".
20. Write an algorithm that, given an input grade, determines if it is correct (between 1 and 10).
x 2
21. Write an algorithm that, given a real value x, calculates the value ; se of the function: y
x x 4
a non-allowed value is entered, show the message 'Impossible'.
22. To write a algorithm what calculations he value of the function:
3
y 1
h x if an invalid value is entered, show the message
z 1 x y
Impossible.
23. Scrivere l'algoritmo per controllare la correttezza di una data ricevuta in ingresso attraverso tre
various inputs: day, month and year; for simplicity, assume all months have 30 days.
1
24. Write the algorithm to check the correctness of a time received as input, through three
different inputs: h, m and s (in 24-hour format).
25. Write an algorithm that, given a user's water consumption, expressed in m3calculate the amount
of the bill, knowing that each bill includes a fixed fee of 20 euros and a share
variable of 2.50 euros/m3for the first 100 cubic meters of water, at 4.00 euros/m3for the cubic meters in
excess
2
MULTIPLE CHOICE
42. Write an algorithm that, given an input value between 1 and 12, displays the name of the month
correspondent.
43. Write an algorithm that, given an input value between 1 and 12, indicates which quarter it belongs to.
of the year corresponds to the month.
44. Write the algorithm to convert an integer N between 1 and 365, provided as input, into
day and corresponding month. Consider a non-leap year.
45. The entrance ticket to a museum has the following prices. For children under 6 years old
The entrance is free (option 1), for students 8 euros (option 2), for retirees 10 euros (option 3)
3), for all the others 15 euros (option 4). Create a program where the user enters a number between
1 and 4 and the price related to the chosen option is communicated. If the number is not a valid option
An error message is displayed.
46. Write an algorithm that, given an input value between 1 and 12, indicates which part
the school year the corresponding month belongs to, considering that from September 1st to 31
December is the first quarter, from January 1 to June 30 is the second half-year and the remaining months.
they are classified as 'summer vacation'.
ENUMERATIVE ITERATION
87. Write an algorithm that, given a number within the range [1, 10], displays its first
10 multiples.
88. Write an algorithm that displays the natural numbers from 300 to 150 in descending order.
89. Write an algorithm that displays the square of the natural numbers between 2 and 20.
90. Write an algorithm that displays the phrase 'Long live summer!' twenty times.
91. Write an algorithm that displays all natural numbers less than the absolute value of a
number chosen by the user.
92. Write an algorithm that displays in ascending order all natural numbers between two
numbers chosen by the user.
93. Write an algorithm that displays in descending order all natural numbers between two
numbers chosen by the user (enforce with mandatory input that the second number entered is
greater than or equal to the first).
94. Write an algorithm that calculates the sum of the first 10 natural numbers.
95. Write an algorithm that calculates the sum of the first N natural numbers; ensure that N > 0.
96. Write an algorithm that takes 10 values as input and calculates the arithmetic mean.
97. Write an algorithm that, given 25 integer values as input, tells how many even values were entered and
how many negative values have been entered.
98. Write an algorithm that, given the input value N>0, calculates the sum of the first N numbers.
positive even.
4
99. Write an algorithm that, given N values (N>0) as input, calculates how many positive values have been provided.
inserted and the percentage of negative values entered.
100. Write an algorithm that, given two integer inputs N and X (with N>0), displays the N
successive integers to X.
101. Write an algorithm that, given two integers N and X (with N>0), displays the N
integer numbers preceding X.
102. Write an algorithm that, given two positive natural numbers X and Y, sums the square roots of
natural numbers between X and Y, extremes included.
103. Write an algorithm that, given N integer values (N>0), calculates how many values are multiples.
of a number chosen by the user.
104. Write an algorithm that, given the input number N (N>0) of values and the threshold value S, calculates
the arithmetic mean of all input numbers that exceed the threshold value.
SUMMARY EXERCISES
105. Given the number of receipts from a merchant's day, calculate the percentage of purchases
less than €10.00 and the average value of purchases.
[Link] the sales target and the collections made by a merchant, calculate whether the target is met.
state reached and, if not, calculate how much the merchant still needs to collect for
reach the goal.
[Link] the quantity and unit price of N types of products sold, calculate the total revenue and the
total number of products taken from the warehouse.
108. Given the names of two athletes and the scores obtained by both in 10 trials, declare the winning athlete.
to communicate the situation of 'equal merit'. Each score must have an integer value.
ranging from 1 to 10; the winner is the one who achieves the highest overall score.
109. Given the scores obtained collectively by 15 athletes, calculate the average score, how many have
obtained an equal score and the percentage of scores below average; store the data in a
object of type ListBox (only for the Visual Basic programming course).
110. Given the subjects and the final grades obtained in each subject at the end of the school year, calculate
the overall average and the number of failures. Therefore showing the outcome of the school year,
considering that without failures you are promoted in June, with less than 4 failures you are
you obtain a suspension of the judgment, otherwise you fail.
Given the names and ages of your friends on a social network, calculate the overall average age and
display the names of friends with ages above average; then calculate the percentage of
friends still underage; for the solution use two ListBox type objects, one for the names and
one for the ages of friends (only for the Visual Basic programming course).