Lab: While Loop
Problems for lab exercise for the course "Programming Basics" course @ SoftUni Global
Submit your solutions to the SoftUni Judge system: https://judge.softuni.org/Contests/4585
1. Read Text
Sample Input and Output
Input Output Input Output
Smith Smith Sofia Sofia
SoftUni SoftUni Berlin Berlin
London London Moscow Moscow
Bulgaria Bulgaria Athens Athens
SomeText SomeText Madrid Madrid
Stop London London
AfterStop Paris Paris
Europe Stop
HelloWorl
d
Testing in the Judge System
Test your solution in the online Judge system: https://judge.softuni.org/Contests/Compete/Index/4585#0
2. Password
Write a program that initially reads the username and password of a user profile. Then reads the login password.
On entering the wrong passwords: ask the user to enter a new password.
On entering the correct password: print "Welcome {username}!".
Sample Input and Output
Input Output Input Output
Smith Welcome, Smith! George Welcome, George!
1234 secret
pass secret
1324
1234
Hints and Guidelines
1. Initialize two variables username and password, which will contain the username and password:
2. Initialize the variable data, which will hold the user's login password:
3. In a while loop, until a valid password is entered, read a new one:
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 1 of 5
4. When a valid password is entered, print the successful login message:
Testing in the Judge System
Test your solution in the online Judge system: https://judge.softuni.org/Contests/Compete/Index/4585#1
3. Sum Numbers
Write a program that reads an integer from the console. This is the target sum. On each following line integers will
be read until their sum of them is greater than or equal to the original number (the target integer). When the
reading is finished, print the sum of the entered numbers.
Sample Input and Output
Input Output Input Output
100 100 20 21
10 1
20 2
30 3
40 4
5
6
Testing in the Judge System
Test your solution in the online Judge system: https://judge.softuni.org/Contests/Compete/Index/4585#2
4. Sequence 2k + 1
Write a program that accepts the number n entered by the user and prints all numbers ≤ n in the sequence: 1, 3, 7,
15, 31... Each following number is calculated by multiplying the previous number by 2 and adding 1.
Sample Input and Output
Input Output Input Output Input Output Input Output
3 1 8 1 17 1 31 1
3 3 3 3
7 7 7
15 15
31
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 2 of 5
Hints and Guidelines
1. Read an integer from the console;
2. Create a variable with an initial value of 1, which you will use as a counter:
3. Create a while loop that repeats until the counter is smaller than the number you read from the console:
4. At each loop iteration, print the counter value and add the given value to it:
Testing in the Judge System
Test your solution in the online Judge system: https://judge.softuni.org/Contests/Compete/Index/4585#3
5. Account Balance
Write a program that calculates how much total money is in a bank account after making a certain number of
deposits. On each line, you will receive the amount you need to deposit into the account until you receive a
"NoMoreMoney" command. For each amount received, the console should output "Increase: " + the
amount and that amount should be added to the account. If a negative number (<0) is received, the console should
output "Invalid operation!" and the program should end. When the program ends, it should print
"Total: " + the total amount in the account, formatted to two digits after the decimal point.
Sample Input and Output
Input Output Input Output
5.51 Increase: 5.51 120 Increase: 120.00
69.42 Increase: 69.42 45.55 Increase: 45.55
100 Increase: 100.00 -150 Invalid operation!
NoMoreMone Total: 174.93 Total: 165.55
y
Testing in the Judge System
Test your solution in the online Judge system: https://judge.softuni.org/Contests/Compete/Index/4585#4
6. Max Number
Write a program that, until it receives the "Stop" command, reads integers entered by the user, finds the largest
among them, and prints it. One number is entered per line.
Sample Input and Output
Input Output Input Output Input Output Input Output Input Output
100 100 -10 20 45 99 999 999 -1 -1
99 20 -20 -2
Stop
80 -30 7 Stop
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 3 of 5
70 Stop 99
Stop Stop
Testing in the Judge System
Test your solution in the online Judge system: https://judge.softuni.org/Contests/Compete/Index/4585#5
7. Min Number
Write a program that, until it receives the "Stop" command, reads integers entered by the user, finds the smallest
among them, and prints it. One number is entered per line.
Sample Input and Output
Input Output Input Output Input Output Input Output Input Output
100 70 -10 -30 45 -20 999 999 -1 -2
99 20 -20 -2
Stop
80 -30 7 Stop
70 Stop 99
Stop Stop
Testing in the Judge System
Test your solution in the online Judge system: https://judge.softuni.org/Contests/Compete/Index/4585#6
8. Graduation
Write a program that calculates the student's average grade from all of his/her studies. On the first line, you will
receive the student's name and on each following line his/her annual grades. The student passes to the next grade
if his annual grade is greater than or equal to 4.00. If he/she failed more than once, he/she will be excluded and the
program ends, printing the student's name and the class he/she was excluded from.
At the successful completion of grade 12 to be printed:
"{student's name} graduated. Average grade: {average grade of all studies}"
In the student is excluded from school, to be printed:
"{student's name} has been excluded at {class he/she was excluded from} grade"
The average grade must be formatted to two digits after the decimal point.
Sample Input and Output
Input Output Input Output
Georg George graduated. Average grade: Mary Mary has been excluded at 8
e 5.53 5 grade
5 6
5.5 5
6 6
5.43 5
5.5 6
6 6
5.55 2
5
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 4 of 5
6 3
6
5.43
5
Testing in the Judge System
Test your solution in the online Judge system: https://judge.softuni.org/Contests/Compete/Index/4585#7
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 5 of 5