Labs and Tasks
Loops
Lab 1:
To do: Print digit 1
ten times.
Expected output:
1 1 1 1 1 1 1 1 1 1
✍Algorithm:
begin
loop 10 do
print(1);
end.
Note: From now on, protect your program against invalid inputs using the Assert()
function.
{0.3 points} Task 1, loop:
To do: Given an integer A
(A > 0
), output the word "Hello
" exactly A
times, separated by commas.
Note: Check whether the number is positive:
assert(a>0,'invalid input; variable a must be > 0');
Expected output:
'Please enter how many times: A=' >>> 3 Hello, Hello, Hello
[Program name: task-01-loop.pas]
{0.3 points} Task 2, loop:
To do: Given two integers K
and N
(N > 0
), output the number K
, repeated N
times.
Note: Verify that both input numbers are positive (use assert()
).
Expected output:
'Enter the number to output: K=' >>> 4 'Enter how many times to output: N=' >>> 3 4 4 4
[Program name: task-02-loop.pas]
Lab 2:
To do: Print numbers from 1
to 10
.
Expected output:
1 2 3 4 5 6 7 8 9 10
{0.4 points} Task 3, loop:
To do: Given an integer A
(A > 0
), output integers between 0
and A
(inclusive) in ascending order and output the number (quantity) of these numbers.
Expected output:
'Enter a number where to stop: A=' >>> 5 0 1 2 3 4 5 quantity = 6
[Program name: task-03-loop.pas]
Lab 3:
To do: Print numbers from 10
down to 1
.
Expected output:
10 9 8 7 6 5 4 3 2 1
{0.4 points} Task 4, loop:
To do: Given an integer A
(A > 0
), output integers between A
and 0
(inclusive) in descending order.
Expected output:
'Enter a number to begin the output:' >>> 5 result: 5 4 3 2 1 0
[Program name: task-04-loop.pas]
Lab 4:
To do: Print powers of 2
starting at 0
; eight powers (1 2 4 8 … 128).
Expected output:
1 2 4 8 16 32 64 128
{0.5 points} Task 5, loop:
To do: Given an integer A
(A > 0
), print 3^A
. You should use multiplication (*). Standard functions are forbidden.
Note: You should use the writelnFormat()
function:
WritelnFormat('3 in the power of {0} = {1}', a, ?);
Expected output:
'Enter a number - power of 3: A=' >>> 4 3 in the power of 4 = 81
[Program name: task-05-loop.pas]
For loops
Lab 5:
To do: Print a sequence of numbers from 1
to 10
.
Expected output:
1 2 3 4 5 6 7 8 9 10
{0.3 points} Task 1, for loop:
To do: Given two integers K
and N
(N > 0
), output the number K
, repeated N
times.
Note: To verify if N > 0
, use the assert()
function:
assert(n > 0, 'bad input, n must be > 0');
Expected output:
'enter the number to output, please: K=' >>> 4 'enter how many times to output: N=' >>> 3 4 4 4
[Program name: task-01-for.pas]
{0.4 points} Task 2, for loop:
To do: Given two integers A
and B
(A < B
), output integers between A
and B
(inclusive) in ascending order and output the number (quantity) of these numbers.
Expected output:
'enter two numbers, please: A= , B=' >>> 0 >>> 5 0 1 2 3 4 5 quantity = 6
[Program name: task-02-for.pas]
Lab:
To do: Output the numbers from 10
down to 1
.
✍Algorithm:
begin
for var i:=10 downto 1 do
begin
print(i)
end;
end.
{0.4 points} Task 3, for loop:
To do: Given two integers A
and B
(A > B
), output integers between values of A
and B
(inclusive) in descending order.
Expected output:
'enter two numbers, please: A= , B=' >>> 5 >>> -2 5 4 3 2 1 0 -1 -2
[Program name: task-03-for.pas]
While and Repeat loops
Lab 6:
To do: Print a sequence of numbers: 0 1 2 3 4
.
Expected output:
0 1 2 3 4
{0.2 points} Task 1, while & repeat loop:
To do: Output the sequence 15 16 17 18 19 20 ... 30
(from 15 to 30). Do it twice: once with a while
loop and then with a repeat
loop.
Note: Use different variables for loop counters.
Expected output:
results with while loop:
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
results with repeat loop:
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
[Program name: task-01-while-repeat.pas]
{0.2 points} Task 2, while & repeat loop:
To do: Output the sequence of integers: 3 5 7 9 ... 21
(from 3 to 21 with a step = 2). Do it twice: once with a while
loop and then with a repeat
loop.
[Program name: task-02-while-repeat.pas]
{0.2 points} Task 3, while & repeat loop:
To do: Output the sequence of integers: 20 18 16 ... 2
(from 20 down to 2 with a step = 2). Do it twice: once with a while
loop and then with a repeat
loop.
[Program name: task-03-while-repeat.pas]
{0.2 points} Task 4, while & repeat loop:
To do: Output the sequence of integers: 15 12 9 6 3 0
(from 15 down to 0 with a step = 3). Do it twice: once with a while
loop and then with a repeat
loop.
[Program name: task-04-while-repeat.pas]
{0.2 points} Task 5, while & repeat loop:
To do: Output the sequence of real numbers: 0.1, 0.3, 0.5, 0.7, 0.9, 1.1
. Do it twice: once with a while
loop and then with a repeat
loop.
[Program name: task-05-while-repeat.pas]
{0.2 points} Task 6, while & repeat loop:
To do: Output the sequence of real numbers: 0.0, 0.5, 1.0, 1.5, 2.0, 2.5
. Do it twice: once with a while
loop and then with a repeat
loop.
[Program name: task-06-while-repeat.pas]
{0.3 points} Task 7, while & repeat loop:
To do: Given two integers A
and B
(A < B
), output integers between A
and B
(inclusive) in ascending order and output the number (quantity) of these numbers. Do it twice: once with a while
loop and then with a repeat
loop.
Expected output:
'enter two numbers, please: A= , B=' >>> -1 >>> 5 results with a while loop: -1 0 1 2 3 4 5 quantity = 7
[Program name: task-07-while-repeat.pas]
Extra tasks
{0.5 points} *Task 8, while loop:
To do: Given positive integers N
and K
, using only the operations of addition and subtraction, find the quotient of dividing N
by K
, as well as the remainder of this division.
Note 1. Use meaningful variable names for quotient and remainder. For example: quotient is quotient
, remainder is remainder
. Possibly: quot
/rem
.
Note 2. Don’t forget to implement protection against invalid input (using Assert()
function).
Expected output:
'N = ' >>> 12 'K = ' >>> 4 quotient = 3, remainder = 0
[Program name: Extask-08-while.pas]
{0.5 points} *Task 9, while loop:
To do: Given positive numbers A
and B
(A ≥ B
), the segment of length A
contains the maximum possible number of segments of length B
(without overlaps). Without using multiplication or division operations, find the length of the unoccupied part of segment A
.
Note 1. To indicate that input values are invalid, use Assert()
functions. Place them after entering the data but before computations begin.
assert((A > 0) and (B > 0));
assert(A >= B);
Expected output:
A = >>> 10 B = >>> 4 result: 2
[Program name: Extask-09-while.pas]