Exercises: Nested Loops
In-class and homework exercises for the "Programming Basics" course @ SoftUni Global.
Test your solutions in the judge system: [Link]
1. Number Pyramid
Write a program, which reads an integer a, entered by the user, and prints a pyramid of numbers like in the Sample
Input and Output:
Input Output Input Output Input Output Input Output
7 1 10 1 12 1 15 1
2 3 2 3 2 3 2 3
4 5 6 4 5 6 4 5 6 4 5 6
7 7 8 9 10 7 8 9 10 7 8 9 10
11 12 11 12 13 14
15
Hints and Guidelines
1. Read one integer from the console:
2. Create two nested for loops, with which to print the pyramid of numbers, as the outer loop will determine
how many rows to print, and the inner - how many numbers are printed on the corresponding row:
3. In a separate counter keep how many numbers you have printed so far (and what is the current number).
When you reach n, exit the two nested loops with break. To get out of both cycles we have to use the
break operator in both of them. For this purpose, we will make a boolean variable to check if we have
exited from the internal loop. Go to the beginning of the program and initialize the following two
variables:
© SoftUni – [Link] Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 1 of 9
4. In the internal for loop, check if the current variable has become greater than n. If so, change the value of
the boolean variable and exit the internal loop:
5. After the checks, print the variable current in the desired format and increase it by 1. If you have left the
loop, printing will not be needed!
6. In the body of the external loop, check whether we need to get out of it too. Then print one blank line so
that the next numbers are on a new line. If we have exited from the external loop, we would not get to
execute the command [Link]()! Your program should look like this:
Testing in the Judge System
Test the solution to this problem here: [Link]
2. Equal Sums Even Odd Position
Write a program, which reads from the console two six-digit integers in the range from 100000 to 300000. The first
number will always be smaller than the second one. Print on the console on 1 line separated by an interval, all
© SoftUni – [Link] Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 2 of 9
numbers which are between the two numbers read by the console at the start and which also meet the following
requirement:
The sum of the numbers of even and odd positions must be equal. If there are no numbers that meet the
requirement, no result is printed on the console.
Sample Input and Output
Input Output Comments
100000 100001 The first number we generate is the number 100000. The sum of the digits of
100050 100012 even positions (yellow) is 0 + 0 + 0 = 0. The sum of the digits of the odd positions
100023 (green) is 0 + 0 + 1 = 1. Because the two amounts are different, the number is
100034 not printed on the console.
100045 The next number is 100001. The sum of even positions is 1 + 0 + 0 = 1, and of
odd positions 0 + 0 + 1 = 1. The two sums are equal and the number is printed.
The next number to check is 100002. It doesn’t meet the requirements and is
not printed
……
With the number 100045, the sum of the even positions is 5 + 0 + 0 = 5, and of
the odd 4 + 0 + 1 = 5. Both amounts are equal the number is printed. Etc.
Input Output Input Output Input Output
123456 123464 299900 299970 299981 299992 100115 No Output
124000 123475 300000 100120
123486
123497
123530
123541
123552
123563
123574
123585
123596
123640
123651
123662
123673
123684
123695
123750
123761
123772
123783
123794
123860
123871
123882
123893
123970
123981
123992
Hints and Guidelines
1. Read the input data from the user:
© SoftUni – [Link] Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 3 of 9
2. To check all the numbers in the interval, create a for loop. After reading the input numbers, set the first
number for the initial value of the control variable. Iterate until the second number is reached by increasing
the value of the control variable by 1:
3. Take the number of the current position as text by concatenating it with an empty string:
4. Make a for loop for going through all digits of the number. Once taken as text, take its length with
.length(). Iterate until the length of the number is reached by increasing the value of the control
variable by 1:
5. Continue to add the logic for finding the sum of even and odd positions for each number. Declare one
variable for the even and odd sum. To get the exact value of the digits, use the method
[Link]().
6. To find digits that are in an even position, use the conditional if construction, check if its index is an even
number by dividing it modularly by 2 (index % 2), if it is even, add it to the sum of the evens if it is not, to the
sum of the odds.
© SoftUni – [Link] Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 4 of 9
7. Once you have found the sum of the digits of even and odd positions, check if they are equal, and if they
are, print the number. Your program should look like this:
Testing in the Judge System
Test the solution to this problem here: [Link]
3. Sum Prime Non-Prime
Write a program, which reads integers from the console, until receiving the command "stop". Find the sum of all
prime numbers and the num of all non-prime numbers. Due to the mathematical definition that negative numbers
cannot be prime, if a negative number is received as input, print the following message "Number is
negative.". In this case, the entered number is ignored and is not added to either of the two sums, and the
program continues its execution, waiting for the next number to be entered. Print two lines of the two calculated
sums in the following format:
"Sum of all prime numbers is: {prime numbers sum}"
"Sum of all non prime numbers is: {nonprime numbers sum}"
© SoftUni – [Link] Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 5 of 9
Sample Input and Output
Input Output Comments
3 Sum of all prime The first entered number is 3. It is prime, so we add it to the sum
9 numbers is: 29 of prime numbers.
0 Sum of all non prime The next number is 9. It is not prime, so we add it to the sum of
7 numbers is: 13 non-prime numbers.
19 The number 0 is not prime, so we add it to the sum of non-prime
4 numbers. The sum is 9+0=9.
stop The next two numbers are 7 and 19. Both are prime, so we add
them to the sum of prime numbers. 3+7=10 and 10+19=29.
The next number is 4, which is not prime, so we add it to the
corresponding sum 9+4=13.
The stop command is received. The program stops its execution
and we print both sums.
Input Output Input Output
30 Number is negative. 0 The number is negative.
83 Sum of all prime -9 Sum of all prime numbers is: 0
33 numbers is: 83 0 Sum of all non prime numbers is: 0
-1 Sum of all non prime stop
20 numbers is: 83
stop
Testing in the Judge System
Test the solution to this problem here: [Link]
4. Train the Trainers
The "Train the trainers" course is coming to an end and the final evaluation is approaching. Your task is to help the
jury that will evaluate the presentations by writing a program in which to calculate the average grade of each of the
student’s presentations, and finally the average grade of all of them.
On the first line of the console, the number of people in the jury is read as n – integer in the range[1…20]
After that, the name of the presentation is read in a new line - text
For each of the presentation, on new lines is read n – the number of grades – a floating-point number in the range
[2.00…6.00]
After calculating the average grade for a specific presentation, print on the console:
"{presentation_title} - {average_grade}."
After receiving the command "Finish" print on the console:
"Student's final assessment is {average grade from all presentations}." and the program stops.
All grades must be formatted two decimal places after the comma.
Sample Input and Output
Input Output Comments
2 While-Loop - 5.75. 2 – the number of the people in the jury, therefore each
© SoftUni – [Link] Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 6 of 9
While- For-Loop - 5.75. presentation will receive 2 grades.
Loop Student's final (6.00 + 5.50) / 2 = 5.75
6.00 assessment is 5.75. (5.84 + 5.66) / 2 = 5.75
5.50 (6.00 + 5.50 + 5.84 + 5.66) / 4 = 5.75
For-Loop
5.84
5.66
Finish
Input Output Input Output
3 Arrays - 4.92. 2 Objects and Classes - 5.00.
Arrays Lists - 5.75. Objects Dictionaries" - 4.82.
4.53 Student's final and RegEx - 3.15.
5.23 assessment is 5.34. Classes Student's final assessment is
5.00 5.77 4.32.
Lists 4.23
5.83 Dictionari
6.00 es
5.42 4.62
Finish 5.02
RegEx
2.88
3.42
Finish
Testing in the Judge System
Test the solution to this problem here: [Link]
5. Special numbers
Write a program, which reads an integer N, given by the user, and generates all possible "special" numbers from
1111 to 9999. In order for a number to be "special", it needs to meet the following condition:
N to be divisible by each of its digits without remainder.
Example: with N = 16, 2418 is a special number:
16 / 2 = 8 no remainder
16 / 4 = 4 no remainder
16 / 1 = 16 no remainder
16 / 8 = 2 no remainder
Input Data
The input is received from the console and consists of one integer in the range [1…600000]
Output Data
Print all "special" numbers on the console, separated by an interval.
Sample Input and Output
Input Output Comments
3 1111 1113 1131 1133 1311 1313 1331 1333 3111 3113 3131 3133 3311 3313 3331 3 / 1 = 3 no remainder
3333 3 / 3 = 1 no remainder
3 / 3 = 1 no remainder
© SoftUni – [Link] Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 7 of 9
3 / 3 = 1 no remainder
11 1111
16 1111 1112 1114 1118 1121 1122 1124 1128 1141 1142 1144 1148 1181 1182
1184 1188 1211 1212 1214 1218 1221 1222 1224 1228 1241 1242 1244 1248
1281 1282 1284 1288 1411 1412 1414 1418 1421 1422 1424 1428 1441 1442
1444 1448 1481 1482 1484 1488 1811 1812 1814 1818 1821 1822 1824 1828
1841 1842 1844 1848 1881 1882 1884 1888 2111 2112 2114 2118 2121 2122
2124 2128 2141 2142 2144 2148 2181 2182 2184 2188 2211 2212 2214 2218
2221 2222 2224 2228 2241 2242 2244 2248 2281 2282 2284 2288 2411 2412
2414 2418 2421 2422 2424 2428 2441 2442 2444 2448 2481 2482 2484 2488
2811 2812 2814 2818 2821 2822 2824 2828 2841 2842 2844 2848 2881 2882
2884 2888 4111 4112 4114 4118 4121 4122 4124 4128 4141 4142 4144 4148
4181 4182 4184 4188 4211 4212 4214 4218 4221 4222 4224 4228 4241 4242
4244 4248 4281 4282 4284 4288 4411 4412 4414 4418 4421 4422 4424 4428
4441 4442 4444 4448 4481 4482 4484 4488 4811 4812 4814 4818 4821 4822
4824 4828 4841 4842 4844 4848 4881 4882 4884 4888 8111 8112 8114 8118
8121 8122 8124 8128 8141 8142 8144 8148 8181 8182 8184 8188 8211 8212
8214 8218 8221 8222 8224 8228 8241 8242 8244 8248 8281 8282 8284 8288
8411 8412 8414 8418 8421 8422 8424 8428 8441 8442 8444 8448 8481 8482
8484 8488 8811 8812 8814 8818 8821 8822 8824 8828 8841 8842 8844 8848
8881 8882 8884 8888
Testing in the Judge System
Test the solution to this problem here: [Link]
6. Cinema tickets
Your task is to write a program, which calculates the percentage of tickets for each kind of the sold tickets:
student(student), standard(standard) и kid(kid), for all screenings. You also need to calculate what percentage of
the hall is filled for each screening.
Input Data
The input is a series of integers and text:
On the first line, until receiving the "Finish" command – Title of the movie– text
On the second line – the free seats in the hall for each screening – integer [1 … 100]
For each movie, read one line until there are no more free seats in the hall, or until the "End" command is
received:
o The type of the ticket – text ("student", "standard", "kid")
Output Data
Print the following lines to the console:
After each movie, print the percentage of taken seats in the hall:
"{Movie title} - {percentage of taken seats}% full."
On receiving the "Finish" command, print for lines:
o "Total tickets: {the sum of all sold tickets for all movies}"
o "{percentage of student tickets}% student tickets."
o "{percentage of standard tickets}% standard tickets."
o "{percentage of kid tickets}% kids tickets."
Sample Input and Output
Input Output Comments
© SoftUni – [Link] Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 8 of 9
Taxi Taxi - 60.00% full. First movie – Taxi, the seats in the hall are 10
10 Scary Movie - 100.00% 3 standard, 2 student, 1 kid tickets are bought until
standard full. receiving the End command.
kid Total tickets: 12
student 66.67% student tickets. There are 6 tickets out of 10 seats -> 60% of the hall is
student 25.00% standard tickets. occupied.
standard 8.33% kids tickets. Second movie – Scary Movie, the seats in the hall are 6
standard 6 student tickets are bought and there are no more free
End seats in the hall.
Scary Movie
6 There are 6 tickets out of 6 seats -> 100% of the hall is
student occupied.
student The command Finish is received.
student The total number of purchased tickets for all movies is 12.
student
For all movies, there are:
student
student 8 student tickets. 8 tickets out of 12 are 66.67%
Finish 3 standard tickets. 3 tickets out of 12 are 25%
1 kid ticket. 1 ticket out of 12 is 8.33%
Input Output Comments
The Matrix The Matrix - 40.00% full. First movie – The Matrix, the seats in the hall are 20
20 The Green Mile - 35.29% 2 standard, 4 student, 2 kid tickets are bought until
student full. receiving the End command.
standard Amadeus - 100.00% full.
kid Total tickets: 17 There are 8 tickets out of 20 seats -> 41.18% of the hall is
kid 41.18% student tickets. occupied.
student 47.06% standard tickets. Second movie - The Green Mile, the seats in the hall are 17
student 11.76% kids tickets. 3 standard, 3 student tickets are bought until receiving the
standard End command.
student There are 6 tickets out of 17 seats-> 47.06% of the hall is
End occupied.
The Green Third movie – Amadeus, the seats in the hall are 3
Mile
3 standard tickets are bought and there are no more free
17
student seats in the hall.
standard There are 3 tickets out of 3 seats-> 100% от of the hall is
standard occupied.
student The command Finish is received.
standard The total number of purchased tickets for all movies is 17.
student For all movies, there are:
End 7 student tickets. 7 tickets out of 17 are 41.18%
Amadeus 8 standard tickets. 8 tickets out of 17 are 47.06%
3
2 kid tickets. 2 tickets out of 17 are 11.76%
standard
standard
standard
Finish
Testing in the Judge System
Test the solution to this problem here: [Link]
© SoftUni – [Link] Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 9 of 9