In the previous chapter, we learned how to run a block of commands more than once. That's why we introduced a for
loop and reviewed some of its main use cases. The purpose of this chapter is to improve our knowledge, by solving some more complex exam problems with loops, used for a practical exams. For some of them, we will show examples of comprehensive solutions, and for others, we will provide only guidance.
Before we start, let's take another look at the for
loop construction:
The for
loops consist of:
- Initialization expression, in which the variable-counter (
let i
) is created and its initial value is set. - Condition expression (
i <= 10
), executed once, before each loop iteration. - Increment expression (
i++
) – expression executed after each iteration. - Body of the loop - contains any block of source code
Let's solve some SoftUni exam problems with loops
N integers are given within the range of [1 … 1000]. A percentage of them p1 are with values below 200, another percentage p2 are with values from 200 to 399, percentage p3 are with values from 400 to 599, a percentage p4 are with values from 600 to 799 and the remaining percentage p5 representing values above 800. Write a program that calculates and prints the percentages p1, p2, p3, p4, and p5.
Example: we are given n = 20 numbers: 53, 7, 56, 180, 450, 920, 12, 7, 150, 250, 680, 2, 600, 200, 800, 799, 199, 46, 128, 65 in the following distribution and visualization:
Group | Numbers | Number count | Persent |
---|---|---|---|
< 200 | 53, 7, 56, 180, 12, 7, 150, 2, 199, 46, 128, 65 | 12 | p1 = 12 / 20 * 100 = 60.00% |
200 … 399 | 250, 200 | 2 | p2 = 2 / 20 * 100 = 10.00% |
400 … 599 | 450 | 1 | p3 = 1 / 20 * 100 = 5.00% |
600 … 799 | 680, 600, 799 | 3 | p4 = 3 / 20 * 100 = 15.00% |
≥ 800 | 920, 800 | 2 | p5 = 2 / 20 * 100 = 10.00% |
The first line (argument) of the input is an integer n (1 ≤ n ≤ 1000), representing the count of lines with numbers that will be passed. On the following n lines (arguments) will be given one integer within the range of [1 … 1000] – based on these numbers the histogram should be calculated.
Print on the console histogram that consists of 5 lines, each of them containing a number between 0% and 100%, formatted with precision two digits after the decimal point (for example 25.00%, 66.67%, 57.14%).
Input | Output | Input | Output |
---|---|---|---|
3 1 2 999 |
66.67% 0.00% 0.00% 0.00% 33.33% |
4 53 7 56 999 |
75.00% 0.00% 0.00% 0.00% 25.00% |
Input | Output | Input | Output |
---|---|---|---|
7 800 801 250 199 399 599 799 |
14.29% 28.57% 14.29% 14.29% 28.57% |
9 367 99 200 799 999 333 555 111 9 |
33.33% 33.33% 11.11% 11.11% 11.11% |
Input | Output |
---|---|
14 53 7 56 180 450 920 12 7 150 250 680 2 600 200 |
57.14% 14.29% 7.14% 14.29% 7.14% |
The program that solves this problem could be divided into three parts:
- Reading the input data – the current problem includes a reading of the number n, followed by n integers, each on a single line.
- Processing the input data – in this case, that means allocating the numbers into groups and calculating the percentage breakdown by groups.
- Print the output – print the histogram in the specified format.
Before we proceed to the actual reading of the input data, we have to declare the variables, in which the input data will be stored:
In the variable n
, we will store the count of numbers that we will need to read. Additionally, we have to declare the variables p1
, p2
, etc., in which we will store the count of numbers for each corresponding group.
Once the variables have been declared we can proceed with input data processing.
To be able to read and distribute each number to its respective group, a for
loop from 0 to n
(count of numbers) will be used. Each loop iteration will read and distribute one single number (currentNum
) to its respective group. To define, if a number belongs to a certain group we have to, perform a check in the respective range. Once the check returns true - we increase the count of numbers in this group (p1
, p2
, etc.) by 1:
After determining the count of numbers in each group we can move on to the main objective and calculate the percentages. For the calculation we will use the following formula:
(group percentage) = (count of numbers in a group) / (count of all numbers) * 100
This formula in the source code would look as:
According to the task assignment, the percentages have to be formatted with precision two digits after the decimal point. Considering this, .toFixed(...)
method should be added to the formula and the first variable would look like this:
p1Percentage = (p1 / n * 100).toFixed(2);
//Add the formula for the rest of the variables
To make it even clearer, let's take a look at the following example:
Input | Output |
---|---|
3 1 2 999 |
66.67% 0.00% 0.00% 0.00% 33.33% |
In this case n = 3
.
For this loop we have:
i = 0
- we read the number 1, which is less than 200 and falls into the first group (p1
), we increase the corresponding group counter by 1.i = 1
– we read the number 2, which again falls into the first group (p1
) and we increase the same group counter by 1.i = 2
– we read the number 999, which falls into the last group (p5
) because it's bigger than 800, and we increase the corresponding group counter by 1.
After reading the numbers, we have in group p1
two numbers and in group p5
one number. For the rest of the groups, we have no numbers. The percentage of each group is calculated by applying the above-mentioned formula.
All we have to do at this point is to print the final results:
You can test your solution here: https://judge.softuni.org/Contests/Practice/Index/934#0.
Lily is N years old already. For each birthday she receives a present. For each odd birthday (1, 3, 5, …, n) she receives toys, and for the even birthdays (2, 4, 6, …, n) she receives money. For her second birthday, she received 10.00 USD, and the amount is increased by 10.00 USD for every subsequent even birthday (2 -> 10, 4 -> 20, 6 -> 30, etc.). Over the years Lily has secretly saved her money. In the years when Lily received money, her brother each time took 1 USD. Lily sold the toys, she got over the years, each one for P USD, and then added the sum to her savings. With her savings, she wanted to buy herself a washing machine for X USD. Write a program, that calculates the amount of Lily's savings and is this sum enough for buying a washing machine.
The program receives 3 numbers (arguments), entered by the user on separate lines:
- Lily's age – integer in the range of [1 … 77].
- Washing machine price – a number in the range of [1.00 … 10 000.00].
- Single toy price – integer in the range of [0 … 40].
In a single line print on the console:
- If Lily has enough money to make the purchase:
- "Yes! {N}" – where N is the money left after the purchase
- If Lily's savings are not enough for making a purchase:
- "No! {M}" – where M is the insufficiency amount
- The numbers N and M must be formatted with precision two digits after the decimal point.
Input | Output | Comments |
---|---|---|
10 170.00 6 |
Yes! 5.00 | First birthday receives toy; 2nd -> 10 USD; 3rd -> toy; 4th -> 10 + 10 = 20 USD; 5th -> toy; 6th -> 20 + 10 = 30 USD; 7th -> toy; 8th -> 30 + 10 = 40 USD; 9th -> toy; 10th -> 40 + 10 = 50 USD. |
21 1570.98 3 |
No! 997.98 | She saved 550 USD. She sold 11 toys 3 USD each. = 33 USD.Her brother has taken 1 USD each year for 10 years = 10 USD. Remaining amount of money 550 + 33 – 10 = 573 USD |
Similar to the previous problem, the solution could be divided into three parts – reading the input data, processing the input data, and printing the output.
Again, we should start with choosing appropriate variable names. For Lily's age(age
), for the washing machine price (washingMachinePrice
) and the single toy price (toyPrice
). In the above code we declare and initialized (assign value to a variable) also variables for the toys count(toysCount
), and money from birthdays (moneyFromBirthdays
):
We use a for
loop to iterate through every Lily's birthday. If the leading variable is an odd number, we increase the count of toys. We can do the parity check using division with the remainder (%
) by 2 – if the remainder is equal to 0, the number is even, and if the remainder is equal to 1 - odd. Inversely, if the leading variable is an even number, that would mean, that Lily has received money so we have to add this amount of money to her savings. Then we Increase the value of the variable moneyFromBirthdays
, i.e. we increase by 10 the sum, that she will receive for her next birthday. At the same time, we subtract 1 USD - the money taken from her brother. To use the decrement operator we have to add two minus signs after the last sign of the variable (moneyFromBirthdays--
):
Probably you will encounter some difficulties calculating the amount of birthday money if you let the bonus money be added in the following way:
moneyFromBirthdays += 10;
The final result would be 10 x 5 = 50, while our goal is to have 10 + 20 + 30 + 40 + 50 = 150. The problem could be solved by declaring an additional variable(bonusMoney
):
bonusMoney += 10;
moneyFromBirthdays += bonusMoney;
Or we can include the value of the variable i
, which counts the loops and then divide it by 2:
moneyFromBirthdays += 10 * i/2;
Then we have to add the money received from the sold toys to Lily's savings.
At this point what is left is to print the results considering the required formating i.e. the sum should be formatted with precision to the second digit after a decimal point:
To avoid declaring additional variables we can use a template literal - ${expression}
. It's a text literal with a specific sequence of characters allowing embedding of expressions. Using it the calculation can be performed and the result to be directly included in the text string.
You can test your solution here: https://judge.softuni.org/Contests/Practice/Index/934#1.
Ivan is 18 years old and just received an inheritance, consisting of X amount of money and a time machine. He decides to return to the year 1800, but he is not certain if the money would be enough for him, to live at that time without working. Write a program, that calculates if Ivan would have enough money, to live without working until a given year (inclusively). Assuming that, for every even (1800, 1802 etc.) year he would spend 12 000 USD. For every odd year (1801,1803 etc.) he would spend 12 000 + 50 * [the age he would have reached in the given year].
The program receives 2 numbers (arguments), entered by the user on separate lines
- Inherited money – a number in the range of [1.00 … 1 000 000.00].
- The year, until he would have to live in the past (inclusively) – an integer in the range of [1801 … 1900].
Print on the console 1 line. The sum must be formatted with precision up to two digits after the decimal point:
- If the money is enough:
- „Yes! He will live a carefree life and will have {N} dollars left.“ – where N is the remaining amount of money.
- If the money is not enough:
- „He will need {M} dollars to survive.“ – where M is the insufficient amount of money.
Input | Output | Explanations |
---|---|---|
50000 1802 |
Yes! Ivan would live a carefree life and would have 13050.00 USD left. |
1800 → even |
100000.15 1808 |
He would need 12399.85 USD to survive. |
1800 → even |
The method for solving this problem is similar to the previous ones, so we should start with declaring and initializing the required variables. According to the problem description, Ivan is eighteen years old, so we could declare the variable years
and set an initial value of 18. The values of the other variables we can read from the function parameters:
Using a for
loop we can iterate through all the years. Starting from 1800 – the year, in which Ivan wants to return, and reach the year until which he has to live in the past. In the loop, we check, if the current year is even or odd. We perform the check using division with a remainder (%
) by 2. If the year is even, we subtract from the (heritage
) 12000, and if the year is odd, we subtract from the (heritage
) 12000 + 50 * (the age he would have reached in the given year):
Finally, we have to print out the result by checking whether the inheritance (heritage
) was enough to live without working or not. If the inheritance (heritage
) is a positive number, we print out: "Yes! He will live a carefree life and will have {N} dollars left.
", and if it's a negative number: "He will need {M} dollars to survive.
". Don't forget to format the result with precision to the second digit after the decimal point.
Hint: Consider using the Math.abs(…)
method, when printing the output in case the inheritance is insufficient.
You can test your solution here: https://judge.softuni.org/Contests/Practice/Index/934#2.
For a certain period, every day patients arrive at the hospital for examination. Initially, the hospital had 7 doctors. Each doctor could treat only one patient per day, but sometimes there is a shortage of doctors, so the remaining patients are sent to other hospitals. Every third day the hospital makes evaluations and if the count of untreated patients is greater than the count of treated ones, another doctor is appointed. Each new doctor is appointed at the beginning of the day before the patients' admission.
Write a program, that calculates for a given period, the count of treated and untreated patients.
On the first line (argument) there is an integer in the range of [1 … 1000] - The period, for which you need to make calculations. On the next lines (arguments) there are integers in the range of [1 … 10 000] – the number of arriving patients, for the current day.
Print on the console 2 lines:
- On the first line: "Treated patients: {count of treated patients}."
- On the second line: "Untreated patients: {count of untreated patients}."
Input | Output | Explanation |
---|---|---|
4 7 27 9 1 |
Treated patients: 23. Untreated patients: 21. |
Day 1: 7 treated and 0 untreated patients for the day |
Input | Output |
---|---|
6 25 25 25 25 25 2 |
Treated patients: 40. Untreated patients: 87. |
3 7 7 7 |
Treated patients: 21. Untreated patients: 0. |
We start again by, declaring and initializing the required variables. The calculation period we read from the console and save in the period
variable. We will need some additional variables such as the number of treated patients (treatedPatients
), the number of untreated patients (untreatedPatients
), and the number of doctors (countOfDoctors
), which initially is set to 7.
Using for
loop we iterate through all days in the given period (period
). For each day, we read from the console the number of the patients(currentPatients
). According to the problem description, the increase of the doctors can occur every third day, but only if the count of untreated patients is greater than the count of treated ones. For this purpose we check, if the day is the third one – using the division with a remainder operator (%
): day % 3 == 0
.
For example:
- If it's the third day, the remainder of the division by 3 will be 0 (
3 % 3 = 0
) and the checkday % 3 == 0
will returntrue
. - If it's the second day, the remainder of the division by 3 will be 2 (
2 % 3 = 2
) and the check will returnfalse
. - If it's the fourth day, the remainder of the division by 1 (
4 % 3 = 1
) and the check will returnfalse
.
If day % 3 == 0
returns true
, the program logic will check, if the number of untreated patients is greater than the treated ones: untreatedPatients > treatedPatients
. If the result is again true
, then the count of doctors will be increased (countOfDoctors
).
Then we have to check if the daily count of patients (currentPatients
) is greater than the count of the doctors (countOfDoctors
). If the count of patients is greater:
- Increase the value of the variable
treatedPatients
by the count of doctors (countOfDoctors
). - Increase the value of the variable
untreatdPatients
by the count of remaining patients, which we calculate by subtracting the count of doctors from the count of patients (currentPatients - countOfDoctors
).
If the count of patients is not greater, we increase only the value of the variable treatedPatients
by the count of patients for the day (currentPatients
).
Finally, the only thing left is to print the number of treated and untreated patients.
You can test your solution here: https://judge.softuni.org/Contests/Practice/Index/934#3.
N integers are given in the range of [1 … 1000]. Some percentage p1 of them are divisible without remainder by 2, percentage p2 are divisible without remainder by 3, percentage p3 is divisible without remainder by 4. Write a program that calculates and prints the percentages p1, p2, and p3. Example: We are given n = 10 numbers: 680, 2, 600, 200, 800, 799, 199, 46, 128, 65 and the following distribution and visualization:
Division without remainder by: | Numbers | Count | Percent |
---|---|---|---|
2 | 680, 2, 600, 200, 800, 46, 128 | 7 | p1 = (7 / 10) * 100 = 70.00% |
3 | 600 | 1 | p2 = (1 / 10) * 100 = 10.00% |
4 | 680, 600, 200, 800, 128 | 5 | p3 = (5 / 10) * 100 = 50.00% |
On the first line (argument) of the input, we have integer n (1 ≤ n ≤ 1000) – count of numbers. On each next n line, we have one integer in the range of [1 … 1000] – numbers that have to be checked for division without remainder.
Print on the console 3 lines, each containing a percentage between 0% and 100%, formatted with precision two digits after the decimal point, for example 25.00%, 66.67%, 57.14%.
- On the first line – the percentage of the numbers, which is divisible by 2.
- On the second line – the percentage of the numbers, which is divisible by 3.
- On the third line – the percentage of the numbers, which is divisible by 4.
Input | Output | Input | Output | Input | Output |
---|---|---|---|---|---|
10 680 2 600 200 800 799 199 46 128 65 |
70.00% 10.00% 50.00% |
3 3 6 9 |
33.33% 100.00% 0.00% |
1 12 |
100.00% 100.00% 100.00% |
For the current and for the next problem you will have to write the program code by yourself, following the given guidelines.
The program logic that solves the current problem is similar to the Histogram problem, that we reviewed above. Therefore we can start with declaring the required variables. Typical variable names could be n
– count of numbers (that we need to read from the console) and divisibleBy2
, divisibleBy3
, divisibleBy4
– additional variables, containing a count of the numbers in the corresponding group.
To read and allocate each number to its corresponding group we have to iterate for
loop from 0
to n
(count of numbers). Each iteration of the loop should read and allocate one single number. The difference here is that one number could be a part of several groups simultaneously, therefore we have to perform three different if
checks for each number - respectively, whether the number is divisible by 2,3, and 4 and then to increase the value of the variable that keeps the count of numbers in the corresponding group.
Note: if-else
construction wouldn't work in this case, because once the condition is true, the code wouldn't perform any further checkings.
Finally, you need to print the obtained results, by following the specified format.
You can test your solution here: https://judge.softuni.org/Contests/Practice/Index/934#4.
In this problem, you are responsible for the logistics of various types of cargo Depending on the weight of each cargo you need a different vehicle and this will cost different prices per ton:
- Up to 3 tons – microbus (200 USD per ton).
- From over 3 to 11 tons – truck (175 USD per ton).
- Over 11 tons – train (120 per ton).
Your task is to calculate the average price per ton of cargo, as well as the percentage of the cargo transported by each vehicle.
The program receives a sequence of numbers (arguments):
- On the first line (argument): the count of cargos that have to be transported – integer in the range of [1 … 1000].
- On the next lines, we pass the weight of the cargo – integer in the range of [1 … 1000].
Print on the console 4 lines, as follow:
- Line #1 – average price per ton for a transported cargo (formatted with precision two digits after the decimal point).
- Line #2 – a percentage of the cargo, transported by microbus (between 0.00% and 100.00%, formatted with precision two digits after the decimal point).
- Line #3 – a percentage of the cargo, transported by truck (between 0.00% and 100.00%).
- Line #4 – a percentage of the cargo, transported by train (between 0.00% and 100.00%).
Input | Output | Explanation |
---|---|---|
4 1 5 16 3 |
143.80 16.00% 20.00% 64.00% |
The microbus is transporting two cargos 1 + 3, a total of 4 tons. The truck is transporting one cargo: 5 tons. The train is transporting one cargo: 16 tons. The sum of all cargos is: 1 + 5 + 16 + 3 = 25 tons. Percentage of the cargo transported by microbus is: 4/25*100 = 16.00% Percentage of the cargo transported by truck is: 5/25*100 = 20.00% Percentage of the cargo transported by train is: 16/25*100 = 64.00% The average price per ton trasported cargo should be: (4 * 200 + 5 * 175 + 16 * 120) / 25 = 143.80 |
Input | Output | Input | Output |
---|---|---|---|
5 2 10 20 1 7 |
149.38 7.50% 42.50% 50.00% |
4 53 7 56 999 |
120.35 0.00% 0.63% 99.37% |
First, we will read the weight of each cargo and sum the amount of cargo transported by microbus, truck and train, then we have to calculate the total tons of transported cargo. The next step is to calculate the prices of each transport type related to the transported tons and the total price. Finally, we will calculate and print the total average price per ton and what part of the cargo as a percentage is transported by each type of vehicle.
We declare the necessary variables such as countOfLoads
– count of the cargos for transportation (we read them from the console), sumOfTons
– the weight amount of all cargos, microbusTons
, truckTons
, trainTons
– variables containing the amount of weight of transported cargo, respectively by the microbus, the truck, and the train.
We still need a**for
loop** from 0
to countOfLoads - 1
, to iterate through all cargo types. For each cargo, we read weight (in tons) and save the value in a variable, such as tons
. Next, we add the weight of the current cargo (tons
) to the sum of all cargo weights (sumOfTons
). Once we have read the weight of the current cargo, we need to determine which vehicle type will be used (microbus, truck or train). For this purpose we should use if-else
check:
-
If the value of the variable
tons
is less than 3, increase the value ofmicrobusTons
by the value oftons
:microbusTons += tons;
-
In case the value of
tons
is less than 11 - increase the value oftruckTons
by the value oftons
. -
If the value of
tons
is more than 11, increase the value oftrainTons
by the value oftons
.
Before printing the output, we have to calculate the percentage of tons, transported by each vehicle and the average price per ton. For the average price per ton, we will declare one more variable totalPrice
, which will sum the total price of all transported cargo (by microbus, truck and train). We can calculate the average price by, dividing totalPrice
by sumOfTons
. Finally, you have to calculate by yourself the percentages of tons, transported by each vehicle, and print the results, keeping the format specified in the description.
You can test your solution here: https://judge.softuni.org/Contests/Practice/Index/934#5.