## Theory

Lection in pdf format

### Nested Loops

Lab 1:

To do: Calculate the value of the function z(x,y) = xy for every x changing in the interval [2;8], and y changing in the interval [2;4].

Expected output:

z(x,y) = 2^2 = 4
z(x,y) = 2^3 = 8
z(x,y) = 2^4 = 16
z(x,y) = 3^2 = 9
z(x,y) = 3^3 = 27
z(x,y) = 3^4 = 81
z(x,y) = 4^2 = 16
z(x,y) = 4^3 = 64
z(x,y) = 4^4 = 256
z(x,y) = 5^2 = 25
z(x,y) = 5^3 = 125
z(x,y) = 5^4 = 625
... etc.


✍ Algorithm:

• We must create two for loops (nested loop): one loop within the other. Variable x has to be modified in the outer loop; variable y has to be modified within the inner loop.
{0.4 points} Task 1, nested loops:

To do: Calculate a function z(x,y) = x+2y for every x varying in the interval [5;10] and y varying in the interval [1;5]. You should use nested for loops.

[Program name: task-01-nested_loops.pas]

Expected output:

z(x,y)= 5+2*1 = 7
z(x,y)= 5+2*2 = 9
z(x,y)= 5+2*3 = 11
z(x,y)= 5+2*4 = 13
z(x,y)= 5+2*5 = 15
z(x,y)= 6+2*1 = 8
z(x,y)= 6+2*2 = 10
z(x,y)= 6+2*3 = 12
z(x,y)= 6+2*4 = 14
z(x,y)= 6+2*5 = 16
z(x,y)= 7+2*1 = 9
z(x,y)= 7+2*2 = 11
z(x,y)= 7+2*3 = 13
z(x,y)= 7+2*4 = 15
z(x,y)= 7+2*5 = 17
z(x,y)= 8+2*1 = 10
z(x,y)= 8+2*2 = 12
z(x,y)= 8+2*3 = 14
z(x,y)= 8+2*4 = 16
z(x,y)= 8+2*5 = 18
z(x,y)= 9+2*1 = 11
z(x,y)= 9+2*2 = 13
z(x,y)= 9+2*3 = 15
z(x,y)= 9+2*4 = 17
z(x,y)= 9+2*5 = 19
z(x,y)= 10+2*1 = 12
z(x,y)= 10+2*2 = 14
z(x,y)= 10+2*3 = 16
z(x,y)= 10+2*4 = 18
z(x,y)= 10+2*5 = 20

{0.4 points} Task 2, nested loops:

To do: Calculate the function z(x,y) = x-y for every x varying in the interval [30;33] and y varying in the interval [1;5]. You should use nested for loops.

Expected output:

30-1=29
30-2=28
30-3=27
30-4=26
30-5=25
31-1=30
31-2=29
31-3=28
31-4=27
31-5=26
32-1=31
32-2=30
32-3=29
32-4=28
32-5=27
33-1=32
33-2=31
33-3=30
33-4=29
33-5=28


[Program name: task-02-nested_loops.pas]

{0.4 points}Task 3_0, nested loops:
To do: The program must display the rows with the following sequences as in the example below. Use nested loops: the outer for loop must iterate over the columns, the inner for loop must iterate over the sequences within each row.

Expected output:

9 9 9 9 9
8 8 8 8 8
7 7 7 7 7
6 6 6 6 6
5 5 5 5 5


[Program name: task-3_0-nested_loops.pas]

{0.4 points} Task 3_1, nested loops:

To do: The program must display the rows with the following sequences as in the example below. Use nested loops: the outer for loop must iterate over the columns, the inner for loop must iterate over the sequences within each row.

Expected output:

0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
6 6 6 6 6
7 7 7 7 7
8 8 8 8 8
9 9 9 9 9


[Program name: task-3_1-nested_loops.pas]

{0.4 points} Task 4, nested loops:

To do: The program must display the rows with the following sequences as in the example below. Use nested loops and arbitrary step (=2).

Expected output:

1 1 1 1 1 1
3 3 3 3 3 3
5 5 5 5 5 5
7 7 7 7 7 7
9 9 9 9 9 9


[Program name: task-04-nested_loops.pas]

{0.7 points} Task 5, nested loops:

To do: Integers K > 0 and N ≥ 0 are given. K sequences of integers with N elements in each set are entered. The program has to output the number of odd elements in each sequence.

Note: It is better to use random function to generate the numbers of sequences. But in this case you shouldn’t forget to print out the elements.

var a:=random(10);

Expected output:

please, enter the quantity of the sequences:
>>> 3
enter the quantity of numbers within the sequences
>>> 5

the sequence #1
4 9 3 3 7 result: 4 odd elements
the sequence #2
6 9 8 2 9 result: 2 odd elements
the sequence #3
5 1 7 3 6 result: 4 odd elements


[Program name: task-05-nested_loops.pas]

{0.7 points} Task 6, nested loops:

To do: Integers K > 0 and N ≥ 0 are given. K sequences of integers with N elements in each set are entered. The program has to output a minimum and maximum elements in each sequence.

Note 1: It is better to use random function to generate the numbers of sequences. But in this case don’t forget to print out the elements.

var a:=random(10);

Note 2: It is better to use the following constants as the initial values of the minimum and maximum variables:

min:=integer.MaxValue; max:=integer.MinValue;

Expected output:

please, enter the quantity of the sequences:
>>>3
enter the quantity of numbers within the sequences
>>>5

the sequence #1
4 9 5 4 8 min =4, max=9

the sequence #2
6 9 2 3 4 min =2, max=9

the sequence #3
9 1 4 8 7 min =1, max=9


[Program name: task-06-nested_loops.pas]

### Boolean type (logical)

Lab 3, boolean type:

To do: The values of n and k are entered. n numbers are entered. The program should output if there is a number k among them. (the result must be of boolean type)

Expected output:

how many numbers (n)?
>>>5
what number must we find (k)?
>>>3
>>>1  >>>3  >>>6  >>>4  >>>7
there is '3' number:  True


✍ Algorithm:

begin var n := ReadInteger('how many numbers?'); assert (n>0); var exists := false; var k := ReadInteger('what number must we find?'); print('enter numbers, please'); loop n do begin var x := ReadInteger; if x = k then exists := true; end; print ($'there is {k} number: ', exists) end. ### Break and continue Lab 4, break: To do: The values of n and k are entered. n numbers of the sequence are entered. The program should output if there is a number k among them? ✍ Algorithm: begin var n := ReadInteger('how many numbers?'); assert (n>0); var exists := false; var k := ReadInteger('what number must we find?'); print('enter numbers, please'); loop n do begin var x := ReadInteger; if x = k then begin exists := true; break; end; end; print ($'there is {k} number: ', exists) end.

To do: Integers K > 0 and N ≥ 0 are given. K sequences of integers with N elements in each set are entered. The program has to output if there is a number 2 among the numbers of sequences and output the total quantity of 2 number.

Expected output:

please, enter the quantity of the sequences:
>>> 3
enter the quantity of numbers within the sequences:
>>> 2

input the sequence #1:
>>> 1 >>> 5

input the sequence #2:
>>> 2 >>> 5

input the sequence #3:
>>> 5 >>> 7

there is a number '2': true
total quantity of '2': 1


[Program name: task-07-bool.pas]

### The Infinite Loop

Examples:

{# 1} while true do begin ... end; end.
{# 2} repeat ... until false;
{# 3} var x:=1; while x>0 do begin ... end; end.
{# 4} var x:=1; repeat ... until x<=0; end.
Lab 5, infinite loops:

To do: A sequence of integers is given. The last number of the sequence is 0 (if 0 is entered the input of the numbers of the set is finished). The program must output the number of positive elements among the sequence.

Expected output:

please, enter the sequence, print 0 if you want to stop:
>>>1 >>>-5 >>>9 >>>5 >>>0
positive:  3

please, enter the sequence, print 0 if you want to stop:
>>>1 >>>-2 >>>-5 >>>-9 >>>11 >>>0
positive:  2


✍ Algorithm:

Solution 1:

begin print('please, enter the sequence, print 0 if you want to stop:'); var count:=0; var x:=readinteger; while x<>0 do begin if x>0 then count+=1; x:=readinteger; end; print($'positive = {count}') end. Solution 2: begin print('please, enter the sequence, print 0 if you want to stop:'); var count := 0; while true do begin var x := readinteger; if x = 0 then break else if x > 0 then count += 1; end; print($'positive = {count}') end.
{0.4 points} Task 8, infinite loops:

To do: A sequence of integers is given. The last number of the sequence is 0 (if 0 is entered the input of the numbers of the set is finished). The program must output the minimum and maximum of them. Use infinite loop.

Expected output:

please, enter the sequence, print 0 if you want to stop:
>>>1 >>>-2 >>>-5 >>>-9 >>>11 >>>0
min = -9  max = 11


[Program name: task-08-infinite_loops.pas]

{0.7 points} Task 9, infinite loops:

To do: Integer K is given (K > 0) and the set of K sequences. The last number of all the sequences is 0 (if 0 is entered the input of the numbers is finished). The program has to output the number of elements in each sequence, and also output the total number of the elements in all the sequences together.

Expected output:

please, enter the quantity of the sequences
>>> 3
input the sequence #1
>>>1  >>>5  >>>8  >>>0
the seq has 3 elements

input the sequence #2
>>>7  >>>2  >>>0
the seq has 2 elements

input the sequence #3
>>>6  >>>0
the seq has 1 elements

Total number of elements:  6


[Program name: task-09-infinite_loops.pas]

### Working with digits of natural number

Lab 6:

To do: Natural number m is given. Calculate sum of its digits
Solution: div / mod operations split the number on the series of its digits

Expected output:

m:
>>> 425
result: 11

✍ Algorithm:

begin var m := ReadInteger; Assert(m > 0); var s := 0; while m > 0 do begin s += m mod 10; // sum of digits m := m div 10; // discard the number to the right end; end.
{0.7 points} Task 10, digits:

To do: An integer is given (variable a). Find the number of its digits and their product.

Note: since entered number can be a negative number, you can use the abs function, to have an absolute value of a number:

abs(m)

Expected output:

N:
>>> 1205
Count = 4, product = 0
--
N:
>>> -111
Count = 3, product = -1


[Program name: task-10-loops.pas]

{0.7 points} Task 11, digits:

To do: Natural number m is given. How many even digits in its decimal representation does it have?

Expected output:

m:
>>> 745
even: 1


[Program name: task-11-loops.pas]

### More about Sequences in while loop

Lab 7, sequences:

To do: A sequence of integers is given. The last number of all the sequences is 0 (if 0 is entered the input of the numbers is finished). The program has to print 0 in the case when the sequence forms a non-increasing sequence of numbers, otherwise the program has to print the number 1.

Expected output:

please, enter the sequence, print 0 if you want to stop:
>>>1 >>>5 >>>9 >>>5 >>>0
output: 0 (non-increasing sequence)
+++++++++++++++++++++++++++++++++++++++++++++++++
please, enter the sequence, print 0 if you want to stop:
>>>1 >>>2 >>>5 >>>9 >>>11 >>>0
output: 1 (increasing sequence)


✍ Algorithm:

Solution 1:

Solution 2:
{0.7 points} (complex) Task 12, loops:

To do: Integer K is given (K > 0) and the set of K sequences. The last number of all the sequences is 0 (if 0 is entered the input of the numbers is finished). The program has to print true in the case when the sequence contains the element which is less than its neighbor on the left, otherwise the program has to print the number false.

Expected output:

'please, enter the quantity of the sequences:' 3

'input the sequence #1:'
1 5 3 0
>>> output: true

'input the sequence #2:'
2 6 0
>>> output: false

'input the sequence #3:'
5 7 4 0
>>> output: true


[Program name: task-12-loops.pas]

{1.0 points} (complex) Task 13, loops:

To do: A sequence is called sawtoothed if each of its elements is either greater than each of its neighbors, or less than them. A sequence of integers containing at least two elements is given. The last number of the sequence is 0. Check if the given sequence is sawtoothed. Output true or false.

Note 1: A sequence of two elements is considered a sawtoothed if these elements are different.

Note 2: To specify if the sequence is either sawtoothed or not, you must use a variable of boolean type:

var flag := true;

Note 3: You do not need to check that the sequence contains at least two elements.

Expected output:

enter the sequence:
>>> 2  >>> 9  >>> 1  >>> 7  >>> 5  >>> 0
sawtoothed: True
---
enter the sequence:
>>> 1  >>> 2  >>> 3  >>> 2  >>> 5  >>> 0
sawtoothed: False


[Program name: task-13-loops.pas]

{0.4 points} Task 14:

To do: An integer N is given and a set of N numbers (they are input). Find a serial number (position) of the first minimum among the numbers and serial number of the last maximum among the numbers.

Expected output:

enter 10 numbers:
>>>2  >>>15  >>>3  >>>8  >>>1  >>>2  >>>9  >>>1  >>>15  >>>11
serial numb of min is 5, serial numb of max is 9


[Program name: task-14.pas]

{0.3 points} Task 15, loops:

To do: n is entered. Print the Fibonacci sequence of n elements.

Expected output:

enter n:
>>> 8
result: 1 1 2 3 5 8 13 21


[Program name: task-15-fib.pas]

## Theory

Lection # 8 in pdf format

Syntax:
The sum in Pascal is calculated by the recurrent expression:

S = S + Y
where S is the accumulated amount
Y – next summand


Lab 1:

To do: 10 numbers are given. It’s required to calculate their sum.

✍ Algorithm:

The sum variable is initialized by 0 before loop. Every loop iteration the variable sum is incremented by a value of the next entered number.
{0.3 points} Task 1, sum:

To do: 5 integers are entered. After entering each number, the program should print their sum.

Expected output:

'please, enter 5 numbers:'
>>> 4
entered number: 4  => sum = 4
>>> 7
entered number: 7 => sum = 11
>>> 3
entered number: 3 => sum = 14
... etc.


[Program name: task-01-sum.pas]

{0.3 points} Task 2, sum:

To do: 5 real numbers are entered. The program should output their sum only once, just before the end of the program.

Expected output:

entered numbers:
>>> 4.2  >>> 7.1  >>> 3.1  >>> 2.5  >>> 8.6
sum = 25.5


[Program name: task-02-sum.pas]

{0.3 points} Task 3, sum:

To do: Count the sum of 10 numbers: 1 + 2 + 3 + … + 10. Better use for loop.

[Program name: task-03-sum.pas]

{0.3 points} Task 4, sum:

To do: Count the sum of 5 numbers: 1 + 3 + 5 + 7 + 9. Better use for loop with an arbitrary step.

Expected output:

'1 + 3 + 5 + 7 + 9 ='
25


[Program name: task-04-sum.pas]

{0.4 points} Task 5, sum:

To do: Calculate a sum of all odd integers up to 99 (1 + 3 + 5 + 7 + 9 + 11 + … + 99). Use for loop with an arbitrary step.

Expected output:

'1 + 3 + 5 + 7 + 9 + ... + 99 ='
2500


[Program name: task-05-sum.pas]

{0.4 points} Task 6, sum:

To do: Calculate a sum of all odd integers up to 99 (1 + 3 + 5 + 7 + 9 + 11 + … + 97 + 99). Use loop.

[Program name: task-06-sum.pas]

Lab 2: Sum – short solution

To do: 10 integers are given. It’s required to calculate their sum.

✍ Algorithm:

{0.3 points} Task 7, sum:

To do: 5 integers are entered. The program should output their sum only once, before the end of the program. Use short form as in the lab.

[Program name: task-07-sum.pas]

### Product (multiplication)

Lab 3:

To do: 10 reals are given. It’s required to calculate their product (multiplication).

✍ Algorithm:

• The product variable is initialized by 1 value before loop. Every loop iteration the product is incremented by the value of the next number.
{0.3 points} Task 1, product:

To do: 5 integers are entered. After entering each number, the program should print their product.

Expected output:

'please, enter 5 numbers:'
>>> 4
entered number: 4  => product = 4
>>> 3
entered number: 3 => product = 12
>>> 5
entered number: 5 => product = 60

... etc.


[Program name: task-01-product.pas]

{0.3 points} Task 2, product:

To do: 5 real numbers are entered. The program should output their product only once, just before the end of the program:

Expected output:

entered numbers:
>>> 4.2  >>> 7.1  >>> 3.1  >>> 2.5  >>> 8.6
product = 1987.503


[Program name: task-02-product.pas]

{0.4 points} Task 3, product:

To do: Calculate a product of 2-digit even integers in the interval [10;30] (10 * 12 * 14 * … * 28 * 30). Use for loop with an arbitrary step.

Expected output:

product = 165412864

[Program name: task-03-product.pas]

Lab 4: Product (multiplication) — short solution

To do: 10 real numbers are given. It’s required to calculate their product.

✍ Algorithm:

{0.1 points} Task 4, product:

To do: 5 real numbers are entered. The program should output their product only once, just before the end of the program:

Expected output:

entered numbers:
>>> 4.2  >>> 7.1  >>> 3.1  >>> 2.5  >>> 8.6
product = 1987.503


[Program name: task-04-product.pas]

{0.3 points} Task 5, product:

To do: 5 integers are entered. The program should output their product only once — just before the end of the program. Use short form as in the example.

Expected output:

enter 5 integers, please:
>>>2  >>>5  >>>1  >>>7  >>>3
product is: 210

[Program name: task-05-product.pas]

### Counters

Lab:

To do: n >= 0 is given. The program should ask to input n integer numbers and find a quantity of odd numbers among entered numbers.

Solution: We’re going to use an if statement in a loop.

Expected output:

how many numbers?
>>>4
enter the numbers:
>>>1  >>>3  >>>8  >>>7
the number of odd:  3 

✍ Algorithm:

begin print('how many numbers?'); var n := ReadInteger; print('enter the numbers:'); var count := 0; loop n do begin var x := ReadInteger; if x mod 2 <> 0 then count += 1; end; print('the number of odd: ', count) end.
{0.2 points} Task 1, counters:

To do: Ouput a sequence: 1 2 3 4 . . . 99 100. Use loop and a variable named counter:
A code snippet:

... var counter:=0; loop 100 do begin ... print(counter); end;

Expected output:

1 2 3 4 5 . . . 99 100


[Program name: task-01-counters.pas]

{0.3 points} Task 2, counters:

To do: Ouput a sequence: 1 2 3 4 . . . 99 100 99 . . . 3 2 1.

Note: Create two loops: the first loop should iterate through 1 2 3 4 . . . 99 100, the second loop through 99 . . . 3 2 1.

Expected output:

1 2 3 4 . . . 97 98 99 100 99 98 . . . 4 3 2 1


[Program name: task-02-counters.pas]

{0.2 points} Task 3, counters:

To do: Ten randomized integers are given. You should use for loop and variable named counter to find the quantity of positive among the numbers.

Note: You should use random function to have randomized sequence of numbers:

var genNumb: integer; //... genNumb:=random (a,b);

Note: you can complete the following code:

begin var genNumb, i: integer; var counter := 0; for i := 1 to 10 do begin genNumb := random(-5, 10); // to do ... // ... end; writeln; print('numbers of positive: ', counter) end.

Expected output:

1 6 10 10 -2 1 -4 4 -4 2
numbers of positive:  7


[Program name: task-03-counters.pas]

### Several counters

Lab:

To do: Pupil has got n grades (marks) for the exams (from 2 to 5), they are given. Calculate a number of each grade (how many “2”, how many “3” …).

Solution: We will use several counters, each counter for each grade.

Expected results

how many grades? 5
4
4
4
2
3
"2"= 1, "3"= 1, "4"= 3, "5"= 0


✍ Algorithm:

begin var n := ReadInteger('how many grades?'); var (c2, c3, c4, c5) := (0, 0, 0, 0); writeln('enter grades, please'); loop n do begin var Mark := ReadInteger; case Mark of 2: c2 += 1; 3: c3 += 1; 4: c4 += 1; 5: c5 += 1; end; end; Print($'"2"= {c2}, "3"= {c3}, "4"= {c4}, "5"= {c5}'); end. {0.4 points} Task 4, counters: To do: Ten randomized integers are given. Use loop and two counters to find the quantity of positive and negative among the numbers. Note: You should use random function to have randomized sequence of numbers: var genNumb: integer; //... genNumb:=random (a,b); Expected output: 1 -5 -12 2 3 9 -1 9 5 -8 counter positive = 6, counter negative = 4  [Program name: task-04-counters.pas] {0.4 points} Task 5, counters: To do: Ten randomized integers are given. Use loop and two counters to find the quantity of even and odd among the numbers. Expected output: 1 -5 -12 2 3 9 -1 9 5 -8 counter even = 3, counter odd = 7  [Program name: task-05-counters.pas] ### Minimum and maximum value Lab: To do: n numbers are given (n >= 0). Find minimum value of these numbers. ✍ Algorithm: Solution 1. Let’s assign the first entered number to min variable. Then, in a loop check if the next entered number is less than min. If it is, so reassign this value to min: begin var n:=readinteger('enter n'); var x := ReadReal; var min := x; loop n - 1 do begin x := ReadReal; if x < min then min := x; end; print($'min = {min}') end.

What is not good: The first value is handled separately.

Solution 2. Let’s set the maximum value of real type (real.MaxValue) to min variable. Then, in a loop check if the first entered number is less than min. If it is, so reassign this value to min.

The idea is that after the first iteration min will be reassigned in any case because x < real.MaxValue:

begin var n:=readinteger('enter n'); var x:real; var min := real.MaxValue; loop n do begin x := ReadReal; if x < min then min := x; end; print($'min = {min}') end. {0.2 points} Task 1, min & max: To do: 5 numbers are entered. Output the maximum and minimum of entered numbers. Note: complete the following code snippet: begin var // ...; var min := integer.MaxValue; var max := integer.MinValue; loop 5 do begin // ... end; print($'min = {min}, max = {max}') end.

Expected output:

enter 5 integers:
>>> 5  >>> 3  >>> 8  >>> 1  >>> 2
maximum is 8, minimum is 1


[Program name: task-01-maxmin.pas]

{0.5 points} Task 2, min & max:

To do: An integer N is given and a set of N rectangles defined by their sides (pairs of numbers a and b for each rectangle). Calculate the areas (S = a * b) of all the rectangles and output minimum area of them.

Expected output:

How many rectangles?
>>> 3
Please, enter the sides of the rectangles:
>>> 2  >>> 3
area is: 6
>>> 1  >>> 5
area is: 5
>>> 4  >>> 2
area is: 8
The minimum area of all rectangles is 5


[Program name: task-02-maxmin.pas]

{0.5 points} Task 3, min & max:

To do: An integer N is given and a set of N rectangles defined by their sides (pairs of numbers a and b). Find the perimeters of all the rectangles and output maximum perimeter of them.

Expected output:

How many rectangles?
>>> 3
Please, enter the sides of the rectangles:
>>> 2  >>> 3
perimeter: 10
>>> 1  >>> 5
perimeter: 12
>>> 4  >>> 2
perimeter: 12
The maximum perimeter of all rectangles is 12


[Program name: task-03-maxmin.pas]

{0.3 points} Task 4, min & max:

To do: Find the minimum of 5 numbers and output it's sequential number (order number). To store the sequential number you must use extra variable.

Note: You should use random function to have randomized sequence of numbers:

var genNumb: integer; //... genNumb:=random (a,b);

Expected output:

5 generated numbers:
5   3   8   1   2
minimum is 1, its position is 4


[Program name: task-04-maxmin.pas]

{0.5 points} Task 5, min & max:

To do: Calculate a minimum and maximum of 10 generated numbers and print the sum of their sequential number (order positions or serial numbers).

Note: You should use random function to have randomized sequence of numbers:

var genNumb: integer; //... genNumb:=random (a,b);

Expected output:

10 generated numbers:
2  15  3  8  1  2  9  1  7  11
max is 15 position is 2, min is 1 position is 8, 2 + 8 = 10


[Program name: task-05-maxmin.pas]

### Sums and products in the while and repeat loops

Lab:

To do: Calculate the sum of the sequence: 11 + 13 + 15 + ... + 99.

Expected results:

sum = 2475


✍ Algorythm:

While loop:

begin var x:=11; var s:=0; while x<=99 do begin s+=x; write(x); x+=2; end; Print($'sum = {s}'); end. Loop: {0.3 points} Task 1, sum and while: To do: Calculate a sum of all odd 2-digit integers (11+ 13 + 15 + 17 + 19 + 21 + ... + 99). Make the task two times - using while and repeat loops. Expected output: while loop. sum = 2475 repeat loop. sum = 2475  [Program name: task-01-sum-while.pas] {0.3 points} Task 2, sum and while: To do: Calculate a product of 2-digit even integers in the range [10;30] (10 * 12 * 14 * ... * 28 * 30). Make the task two times - using while and repeat loops. Expected output: while loop. product = 667418624 repeat loop. product = 667418624  [Program name: task-02-sum-while.pas] {0.3 points} Task 3, sum and while: To do: 5 real numbers are entered. The program should output their sum only once, just before the end of the program. Make the task two times - using while and repeat loops. Expected output: please enter 5 numbers: >>> 4.2 >>> 7.1 >>> 3.1 >>> 2.5 >>> 8.6 sum = 25.5  [Program name: task-03-sum-while.pas] {0.3 points} Task 4, sum and while: To do: 5 numbers are entered. The program should output their product (multiplication) only once, just before the end of the program. Make the task two times - using while and repeat loops. Expected output: please enter 5 numbers: >>> 4 >>> 7 >>> 3 >>> 2 >>> 8 multiplication = 1344  [Program name: task-04-sum-while.pas] ## Lesson #7. Loops. Arbitrary step ## Theory Lection # 7 in pdf format ## Labs and tasks ### How to use arbitrary step in for loop & loop Lab 1: To do: Output all 2-digit odd numbers from 11 to 21. Expected output: 11 13 15 17 19 21 ✍ Algorithm: • Solution 1. With loop • Solution 2. With for loop {0.4 points} Task 1: To do: Output the sequence 3 5 7 9 … 21 (from 3 to 21 with a step = 2). Make it twice: with loop and for loop. The fragment of the program: begin println('the result with a loop'); var ...; loop ...; ... println('the result with a FOR loop'); for var ... ... end. Expected output: the result with a loop 3 5 7 9 11 13 15 17 19 21 the result with a FOR loop 3 5 7 9 11 13 15 17 19 21  [Program name: L7-task-01.pas] {0.4 points} Task 2: To do: Output the sequence of integers: 20 18 16 … 2 (from 20 downto 2 with a step = 2). Make it twice: with loop and for loop. Expected output: the result with a loop 20 18 16 14 12 10 8 6 4 2 the result with a FOR loop 20 18 16 14 12 10 8 6 4 2  [Program name: L7-task-02.pas] {0.4 points} Task 3: To do: Output the sequence of integers: 15 12 9 6 3 0 (from 15 downto 0 with a step = 3). Make it twice: with loop and for loop. Expected output: the result with a loop 15 12 9 6 3 0 the result with a FOR loop 15 12 9 6 3 0  [Program name: L7-task-03.pas] {0.4 points} Task 4: To do: Output the sequence of integers: 5 10 15 20 … 100 (from 5 to 100). Make it twice: with loop and for loop. [Program name: L7-task-04.pas] ### Functions z(x) Lab 2: To do: Calculate a value of the function z(x) = x3 for all x varying on the interval [1, 7] with a step = 2. Make it twice: with loop and for loop. Expected output: the result with a loop 1*1*1 = 1 3*3*3 = 27 5*5*5 = 125 7*7*7 = 343 the result with a FOR loop 1*1*1 = 1 3*3*3 = 27 5*5*5 = 125 7*7*7 = 343  ✍ Algorithm: • Solution: {0.5 points} Task 5: To do: Calculate a value of the function z(x) = x2 for all x varying on the interval [3, 12] with a step = 3. Make it twice: with loop and for loop. Expected output: the result with a loop 3*3 = 9 6*6 = 36 9*9 = 81 12*12 = 144 the result with a FOR loop 3*3 = 9 6*6 = 36 9*9 = 81 12*12 = 144  [Program name: L7-task-05.pas] {0.5 points} Task 6: To do: Calculate a value of the function z(x) = √x for all x varying on the interval [5, 25] with a step = 5. Make it twice: with loop and for loop. Note: To calculate the square root of a number in pascal there is a standard function sqrt(x), for example: var result:=sqrt(2); // the result = 1.4142135623731 Expected output: the result with a loop sqrt(5) = 2.23606797749979 sqrt(10) = 3.16227766016838 sqrt(15) = 3.87298334620742 sqrt(20) = 4.47213595499958 sqrt(25) = 5 the result with a FOR loop sqrt(5) = 2.23606797749979 sqrt(10) = 3.16227766016838 sqrt(15) = 3.87298334620742 sqrt(20) = 4.47213595499958 sqrt(25) = 5  [Program name: L7-task-06.pas] {0.5 points} Task 7: To do: Calculate a value of the function z(x) = 2x-2 for all x varying on the interval [5, 20] with a step = 3. Make it twice: with loop and for loop. Expected output: 2*5-2 = 8 2*8-2 = 14 2*11-2 = 20 2*14-2 = 26 2*17-2 = 32 2*20-2 = 38  [Program name: L7-task-07.pas] {0.5 points} Task 8: To do: Calculate a value of the function z(x) = sin(x) for all x varying on the interval [1, 5]. Note 1: To calculate the sin() in pascal there is a standard function sin(x), for example: var result:=sin(2); // the result = 0.909297426825682 Note 2: To output the floating point number with a particular number of digits: writelnFormat('{0:f2}',3.1415); // output: 3.14 Expected output: 'the rezult of z(x) : ' 0.84 0.91 0.14 -0.76 -0.96  [Program name: L7-task-08.pas] ### How to use real step in for & loop Lab : To do: Output the sequence: 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0. ✍ Algorithm: • Solution 1. With loop • Solution 2. With for loop {0.4 points} Task 9: To do: Output the sequence 0.1 0.3 0.5 0.7 0.9 1.1. Make it twice: with loop and for loop. Expected output: the result with a loop 0.1 0.3 0.5 0.7 0.9 1.1 the result with a FOR loop 0.1 0.3 0.5 0.7 0.9 1.1  [Program name: L7-task-09.pas] {0.4 points} Task 10: To do: Output the sequence: 0.0 0.5 1.0 1.5 2.0 2.5. Make it twice: with loop and for loop. Expected output: the result with a loop 0.0 0.5 1.0 1.5 2.0 2.5 the result with a FOR loop 0.0 0.5 1.0 1.5 2.0 2.5  [Program name: L7-task-10.pas] {0.4 points} Task 11: To do: A real number is given— the price of 1 kg of candy. The program has to output the prices of 1, 2, …, 10 kg of candy. Use for loop. Note. Use friendly variable names (it’s better to use writelnFormat statement). Expected output: 'enter a price of 1 kg of candy, please: ' >>> 150.5 the cost of 1 kg = 150.5 the cost of 2 kg = 301 the cost of 3 kg = 451.5 ... the cost of 10 kg = 1505  [Program name: L7-task-11.pas] {0.4 points} Task 12: To do: A real number is given— the price of 1 kg of candy. The program has to output the prices of 1.2, 1.4, 1.6 1.8, 2 kg of candy. Note. Use friendly variable names (it’s better to use writelnFormat statement). Expected output: 'enter a price of 1 kg of candy, please: ' >>> 100.2 the cost of 1.2 kg = 120.24 the cost of 1.4 kg = 140.28 ... the cost of 2 kg = 200.4  [Program name: L7-task-12.pas] {0.4 points} Task 13: To do: Two integers A and B are given. Print the squares of all integers between A and B in ascending order and including these numbers themselves. Expected output: 'enter two integers, please: A = , B = ' >>> 1 >>> -2 4, 1, 0, 1  'enter two integers, please: A = , B = ' >>> 2 >>> 3 4, 9  'enter two integers, please: A = , B = ' >>> 2 >>> 2 4  [Program name: L7-task-13.pas] {0.4 points} Task 14: To do: Two integers A and B are given. Print the square roots of all integers between A and B in ascending order and including these numbers themselves. Expected output: 'enter two integers, please: A = , B = ' >>> 1 >>> 5 sqrt(1) = 1 sqrt(2) = 1.4142135623731 sqrt(3) = 1.73205080756888 sqrt(4) = 2 sqrt(5) = 2.23606797749979  [Program name: L7-task-14.pas] ### Extra tasks Lab: To do: Using one loop, calculate the value of the sum given by the following formula (N> 0): Expected output: Please input real number X >>> 3 Please input integer N > 0 >>> 5 sum of the sequence = 10.2857142857143  ✍ Algorithm: begin var X := ReadReal('Please input real number X '); var N := ReadInteger('Please input integer N > 0'); Assert(N > 0, 'N<=0'); var sum:=0.0; sum := 0.0; var num1:=-x; var num2:=x; var counter:=1; for var i:=1 to n do begin counter*=i; sum+=(num1+num2)/(i+counter); num1*=-x; num2*=x; end; writeln($'sum of the sequence = {sum}'); end.
{0.5 points} Extra task 1:
To do: Using one loop, calculate the value of the sum given by the following formula (N> 0):

Expected output:

Please input real number X
>>> 5
Please input integer N > 0
>>> 2
sum of the sequence = 2.78693528693529


[Program name: L7-extask-1.pas]

{0.5 points} Extra task 2:
To do: Using one loop, calculate the value of the sum given by the following formula (N> 0):

Expected output:

Please input real number X
>>> 5
Please input integer N > 0
>>> 5
sum of the sequence = -491.5


[Program name: L7-extask-2.pas]

## Theory

Lection # 6 in pdf format

• Loops are used to repeat actions
• The loop consists of: a loop header and a loop body
• One loop iteration is one repetition of the loop body
• The simpliest type of loops is loop.
Syntax:

loop n do
some operator;


### For loop

• For loop is a loop with a counter
• After each iteration counter increments by 1

### While and Repeat loop

Syntax of While loop:

while condition do
statements


Syntax of Repeat loop:

repeat
statements
until condition


### 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: Since this moment you should implement protection against invalid input with Assert function.
{0.3 points} Task 1, loop:
To do: An integer A (A > 0) is given. Output the word «Hello» A times. You need to put commas between the words.

Note: You should check if the number is positive:

assert(a>0,'a bad input; the 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: Two integers K and N (N > 0) are given. The number K should be output N times (loop must be used)

Note: You should check if input numbers are positive (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


✍ Algorithm:

{0.4 points} Task 3, loop:
To do: An integer A (A > 0) is given. Output integers between 0 and A (including the value of A) 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

'Enter a number where to stop: A= '
>>> 3
0 1 2 3 quantity = 4


[Program name: task-03-loop.pas]

Lab 3:

To do: Print numbers from 10 downto 1.

Expected output:

10 9 8 7 6 5 4 3 2 1


✍ Algorithm:

{0.4 points} Task 4, loop:
To do: An integer A (A > 0) is given. Output integers between A and 0 (including the value of A) in discending order.

Expected output:

'Enter a number to begin the output:'
>>> 5
result: 5 4 3 2 1 0

'Enter a number to begin the output:'
>>> 3
result: 3 2 1 0


[Program name: task-04-loop.pas]

Lab 4:

To do: Print a power of 2 starting at 0; eight powers (1 2 4 8 … 128).

Expected output:

1 2 4 8 16 32 64 128


✍ Algorithm:

{0.5 points} Task 5, loop:

To do: An integer A (A > 0) is given. Print 3^A (3 in a power of A). You should use multiplication (*). It is forbidden to use standard functions.

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

'Enter a number - power of 3: A= '
>>> 2
3 in the power of 2 = 9


[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


✍ Algorithm:

{0.3 points} Task 1, for loop:

To do: Two integers K and N (N > 0) are given. The number K must be output N times.

Note: To check if N > 0 you should use 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: Two integers A and B are given (A < B). Output integers between A and B (including the values of A and B themselves) in ascending order and output the number (quantity) of these numbers. To check if A < B you should use assert function.

Expected output:

'enter two numbers, please: A= , B ='
>>> 0  >>> 5
0 1 2 3 4 5  quantity = 6

'enter two numbers, please:  A= , B ='
>>> 2  >>> 7
2 3 4 5 6 7 quantity = 6


[Program name: task-02-for.pas]

Lab:

To do: Output the numbers from 10 downto 1 (10 9 8 7 6 5 4 3 2 1).

✍ Algorithm:

 begin for var i:=10 downto 1 do begin print(i) end; end.

{0.4 points} Task 3, for loop:

To do: Two integers A and B are given (A > B). Output integers between values of A and B (including A and B themselves) in discending order. To check if A > B you should use assert function.

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 outpute:

0 1 2 3 4


✍ Algorithm:

{0.2 points} Task 1, while & repeat loop:

To do: Output the sequence 15 16 17 18 19 20 ... 30 (from 15 to 30). Make it twice: with while loop and with repeat loop.

Note: you should use different variables for loops 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). Make it twice: with while loop and with 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 downto 2 with a step = 2). Make it twice: with while loop and with 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 downto 0 with a step = 3). Make it twice: with while loop and with 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. Make it twice: with while loop and with 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. Make it twice: with while loop and repeat loop.

[Program name: task-06-while-repeat.pas]

{0.3 points} Task 7, while & repeat loop:

To do: Two integers A and B are given (A < B). Output integers between A and B (including A and B themselves) in ascending order and output the number (quantity) of these numbers. Make it twice: with while loop and 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
results with a repeat loop:
-1 0 1 2 3 4 5  quantity = 7


[Program name: task-07-while-repeat.pas]

{0.5 points} * Task 8, while loop:

To do: Positive integers N and K are given. 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 meaningfull 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 (Assert function).

Expected output:

'N = '
>>> 12
'K = '
>>> 4
quotient = 3, remainder = 0

'N = '
>>> 27
'K = '
>>> 5
quotient = 5, remainder = 2


[Program name: Extask-08-while.pas]

{0.5 points} * Task 9, while loop:

To do: Positive numbers A and B (A >= B) are given. The segment of length A contains the maximum possible number of segments of length B (without overlays). Without using the multiplication (*) and division(/, div) operations, find the length of the unoccupied part of segment A.

Note 1. To specify that input values are not valid, use Assert functions. They must be placed after the data is entered, but before the calculations begin.

Assert((A > 0) and (B > 0));
Assert(A >= B);


Expected output:

A =
>>> 10
B =
>>> 4
result: 2

A =
>>> 12
B =
>>> 4
result: 0


[Program name: Extask-09-while.pas]

## Theory

### Search for minimum and maximum

• Which of the two variables is greater (maximum):
• var (x,y) := ReadInteger2; var Max: integer; if x>y then Max := x else Max := y;
• Which of the two variables is greater (maximum) and which one is less (minimum):
• var (x,y) := ReadInteger2; var Min,Max: integer; if x>y then begin // don't forget about compound statement! Max := x; Min := y; end else begin Max := y; Min := x; end;
It is very important here not to forget about compound statement begin..end.

### Random function

• Random function is often used in Pascal.
• To generate numbers from 0 to n not including the value of n itself (integers in the interval [0,N)), you must write:
• var genNumb:=random (n);
• To generate numbers in the range [a,b], you must write:
• var a:=readinteger(); var b:=readinteger(); var genNumb:=random (a,b);

To generate Tuple of two elements (Random2), or three elements (Random3):

var (a, b, c) := Random3(10.0, 20.0); // range [10, 20) write(a:0:2,' ',b:0:2,' ', c:0:2) // output: 14.73 18.63 19.72

### Nested If statements. Using Assert function

Example:

To do: A Point (x,y) on а coordinate plane is given ( ≠ 0, ≠ 0). Output a number of quarter:

✍ Решение:

var (x,y) := ReadInteger2; var quarter: integer; Assert((x<>0) and (y<>0), 'incorrect input'); // must return True if x>0 then if y>0 then quarter := 1 else quarter := 4 else if y>0 then quarter := 2 else quarter := 3;

### Maximum and Minimum

{0.3 points} Task 1:
To do: Three integers are entered (they should be in the range from 0 to 10). Find the maximum (the greatest) number among the three entered numbers. Check entered values using assert function.

Expected output:

please enter three integer numbers
>>> 6   >>> 1   >>> 9
The maximum number is 9

please enter three integer numbers
>>> 1   >>> 2   >>> 13
Incorrect input! must be in the range from 0 to 10


{0.3 points} Task 2:
To do: Two real numbers are given. Find a maximum (the greatest) and minimum (the least) number among them. Output max and min.

The resulting example:

please enter three integer numbers
>>> 3.0    >>> 55.5
Maximum number: 55.5  Minimum number: 3.0


{0.5 points} Task 3:
To do: A two-digit number is given. Find the minimum and the maximum among its digits and swap the digits in the number. Check entered values using assert function (it should be two-digit number).

Expected output:

please enter two-digit number
>>> 74
max = 7, min = 4, swapped = 47

please enter two-digit number
>>> 7
Incorrect input! should be two-digit number


{1 point} Task 4:
To do: The integers A, B and k are given. At first output the larger of A and B, then the lesser of them. Print True if the difference between the numbers does not exceed the value of k and print False otherwise. Check the correctness of your program with at least three input data sets, give the log of the program in the form of a comment.

expected output:

Please enter three integer numbers
>>> 3   >>> 5    >>> 2
5 3 True

Please enter three integer numbers
>>> 44   >>> 20   >>> 9
44 20 False


{1 point} Task 5:
To do: A three-digit integer is given. Find the maximum (the greatest) digit and minimum (the least) digit among the three digits of the given number and swap them. Output the numbers. Check entered values using assert function (it should be three-digit number).

The resulting example:

please enter three-digit integer
>>> 167
After swaping maximum and minimum we have: 761

please enter three-digit integer
>>> 7
Incorrect input! should be three-digit number


### If statements (continuation) and random function

{0.4 points} Task 6:
To do: For a given real x find the value of the following function f.

The resulting example:

please enter a real number
>>> -4
The result of function f is 4
---
>>> 1.5
The result of function f is 2.25


{0.3 points} Task 7:
To do: An integer is given. Use random function to generate this number. Display its description in the following form: “negative even number” or “negative odd number” or “zero number” or “positive odd number” or “positive even number”. Check the correctness of your program, provide a log of the program in the form of a comment.

The resulting example:

generated number:
-4
negative even number
---
generated number:
-3
negative odd number


{0.4 points} Task 8:
To do: The numbers X and Y are given. Print True if the point with coordinates (X, Y) lies in the fourth coordinate quarter and print False otherwise. Do not use conditional if statement.

The resulting example:

Please enter the values of x and y
>>> 8   >>> -4
True
---
Please enter the values of x and y
>>> -9  >>> 3
False


{0.4 points} Task 9:
To do: The coordinates X and Y of the chessboard field are given (integers lying in the range of 1–8). Use random function to generate these numbers. Considering that the bottom left cell of the board (1, 1) is black, output True if the field of X and Y is white and print False otherwise.

The resulting example:

Please enter the values of x and y between 1 and 8 inclusive
2 7
False
---
Please enter the values of x and y between 1 and 8 inclusive
3 5
True


{0.3 points} Task 10:
To do: Three integer numbers are given. Use random function to generate these numbers. Print true if none of these numbers are positive and False otherwise. Check the correctness of your program with at least three input data sets.

The resulting example:

generated numbers:
-9 -6 -49
True
---
generated numbers:
0 77 -5
False


{0.3 points} Task 11:
To do: Integers x, y are given. Calculate the value of the function:.

A snippet of code:

begin var x, y: integer; // arguments of f function Write('Input integers x, y: '); Readln(x, y);   var f: integer; // TODO: set the value of the function f(x, y) to f variable   WritelnFormat('f({0}, {1}) = {2}', x, y, f); end.

The resulting example:

Input integers x, y:
>>> 7    >>> -5
f (7, -5) =  70


Note: tasks should be saved in a file with the name of the task, and be sure to insert a comment with the statement of the task in the code.

BOOLEAN

1. Boolean4. Given two integers A and B, verify the following proposition: «The inequalities A > 2 and B ≤ 3 both are fulfilled».
Expected output:
<< A = 1 B = 2
results:
false

2. Boolean5. Given two integers A and B, verify the following proposition: "The inequality A ≥ 0 is fulfilled or the inequality B < −2 is fulfilled".
Expected output:
<< A = 0 B = -3
results:
true

3. Boolean6. Given three integers A, B, C, verify the following proposition: "The double inequality A < B < C is fulfilled".
Expected output:
<< A = 50 B = 66 c = 73
results:
true

4. Boolean7. Given three integers A, B, C, verify the following proposition: "The number B is between A and C".
5. Boolean17. Given a positive integer, verify the following proposition: "The integer is a three-digit odd number".
6. Boolean18. Verify the following proposition: "Among three given integers there is at least one pair of equal ones".
Expected output:
<< -1   -5   3
results:
false


IF

1. If4. Three integers are given. Find the amount of positive integers in the input data.
Expected output:
<< -10   -7   -11
results:
0

2. If6. Given two real numbers, output the larger value of them.
3. If13. Given three real numbers, output the value between the minimum and the maximum.
4. If15. Given three real numbers, output the sum of two largest values.
5. If24. Given a real independent variable x, find the value of a real function f defined as:
f(x) =
2·sin(x), if x > 0,
6 − x, if x ≤ 0.

Expected output:
<< x = 6.05
results:
-0.46

6. If26. Given a real independent variable x, find the value of a real function f defined as:
 	 	−x,	if x ≤ 0,
f(x)	 = 	x2,	if 0 < x < 2,
4,	if x ≥ 2.

Expected output:
<< x = 1.05
results:
1.10


## Theory

Lection # 3 in pdf format

### IF Statements

• If statements are concerned with Boolean logic. If the statement is true, the block of code associated with the if statement is executed. If the statement is false, control either jumps to the line after the if statement, or after the end keyword of an if statement block.
• var numb:= 1; if numb = 1 then begin // statements that will execute if the value of the numb variable is 1, // will be placed here. end;
• You can remove the begin and end keywords if your statement to execute is a single line statement. PascalAbc understands that if no begin and end are used, the line immediately after the if (condition) will be executed if the condition is true.
• Else clauses
• IF statements can also have associated else clauses. The else clause executes when the if statement is false:
• var numb:= 1; if numb = 1 then begin // Block of code executes if the value of the numb variable is 1. end else begin // Block of code executes if the value of the numb variable is not 1. end
Else if clauses (or Chained If statements)
• If statements can also have associated else if clauses. The clauses are tested in the order that they appear in the code after the if statement. If any of the clauses returns true, the block of code associated with that statement is executed and control leaves the block of code associated with the entire if construct.
• var numb:= 1; if numb = 1 then begin // Block of code executes if the value of the numb variable is 1. end else if numb = 0 then begin // Block of code executes if the value of the numb variable is 0. end else begin // Block of code executes if the value of the numb variable is neither above answers. end
• The semicolon before else is not needed!
• You can create as many else if blocks as necessary.
• Sample:
To do: A number of season (Winter is the first) is given. Output a name of the entered season number. It is better to use the chained If statements.

✍ Algorithm:

### Logical (boolean) operations

We can define variables of boolean type:

var A,B: boolean; A := True; B := False; Print(A and B); Print(A or B); Print(not A);
• A and B is True if A is True and B is True at the same time. In other cases A and B is False
• A or B is False if A is False and B is False. In other cases A or B is True
• not A has opposite value: not A is True if A is False
• Logical (boolean) operations in if statements:
• Conditions may consist of logic operations: not, or, and. If there are more than one condition, so each condition must be surrounded by round brackets.
• For example if we must check two conditions:

if (year < 20) or (year > 18) then begin // if body end
If one of the conditions or both conditions are True then if body executes.
• Or another example:
• var a: = 5; if (not (a<4)) and (7>5) then // ← True begin // if body end
• The statements write and print can also return True or False:
• //... a = 5; write (a >= 5); // returns True // ...
The «most popular» errors:
• You don’t need to write semicolon before else clause.
• Don’t forget about begin..end in the cases when there is more than one lines of code after then or else.

### Case statement

If there are too many else if statements, code can become difficult to understand. In this case, a better solution is to use a switch statement:

var numb:= 1; case numb of 1,2 : writeln ('1 or 2'); // Block of code executes if the value of numb is 1 or 2. 3: writeln('3'); // Block of code executes if the value of numb is 2. 4: writeln('4'); // Block of code executes if the value of numb is 3. 5: writeln('5'); // Block of code executes if the value of numb is 5. else writeln('nothing matches'); // Block of code executes if nothing matches. end
• A block labeled else will execute when none of the other blocks match.

### Using String type

Case statement can check the variable of String type.

Lab 1 Example:
Given: the english words ‘dog’, ‘use’, ‘find’.
To do: Output translations of the words into russian.

✍ Algorithm:

### Using ranges and enumerations

Lab 2 Example:
Given: Month ordinal.
To do: Output corresponding name of a season.

✍ Algorithm:

### True or false?

{0.3 points} Task 1:
To do: Two integers are given. Check if the following statement is True: the first number is greater than the second (the program must return True if it is true, and False otherwise).

Expected output:

please enter two integers
>>> 23
>>> 1
23 is greater than 1 is True


{0.3 points} Task 2:
To do: Two integers are entered. Check the truth of the statement: the first number is not equal to the second (the program must return True if it is true and False otherwise).

Expected output:

please enter two integers
>>>5
>>>5
5 is not equal to 5 is False


{0.3 points} Task 3:
To do: Three integers are given: the values of variables A, B, C. Check the truth of the double inequality A < B < C. Make sure that your program is correct with at least two input data sets, give the log of the program in the form of a comment.

Expected output:

please enter three integers
>>>3 >>>6 >>>2
3 is less than 6 less than 2 is False

please enter three integers
>>>2 >>>5 >>>7
2 is less than 5 less than 7 is True


{0.6 point} Task 4:
To do: Three-digit integer is given. Check the truth: the first digit (left digit) of the number is less than the second (middle) and third (right).

Expected output:

please enter a three digit number
>>> 854
8 is less than 5 and 4 is : False


{0.6 point} Task 5:
To do: Two integers are entered. Check the truth of the statement: at least one of these numbers is odd.

Note. Use the odd standard function:

// The function odd returns True when its argument is odd integer:
print (odd(5)); // true
print (odd(6)); // false .


Check the following:

-5,  8 => True
12, 0 => False
6, -1 => True
11, 7 => True


Expected output:

please enter two integer numbers
>>> 9 >>> 2
Either 9 or 2 is an odd number. This is : True


### If statement

{0.3 points} Task 6:
To do: An integer is entered. If this is positive number, you should add 1 to it. Output the result.

Check the following:

-3 => -3
0 =>  0
1 =>  2
5 =>  6


Expected output:

please enter an integer number
>>> -77
The result is -77


{0.3 points} Task 7:
To do: An integer is given. If this is even number then multiply it by 10. Output the result.

Check the following:

2   => 20
1   =>  1
-10 => -100


Expected output:

please enter an integer number
>>> 8
The result is 80


{0.5 points} Task 8:
To do: An integer is given. If this is an even number, then multiply it by 3, if it is not even, then divide it by 3. Output.

Check the following:

4 => 12
9 =>  3
-10 => -30


Expected output:

please enter an integer number
>>> 12
The result is 36


{0.5 points} Task 9:
To do: An integer is given. If this is a positive number then add 1 to it; otherwise subtract it by 2. Output.

Check the following:

-3 => -5
0 =>  1
1 =>  2
5 =>  6


Expected output:

please enter an integer number
>>> 48
The result is 49


### Chained If statements and Logical operations in if statements

{0.5 points} Task 10:
To do: An integer is given (age of the person). If it is greater than or equal to 18, then output "you can watch this movie"; if the number is less than 10, then output "you should watch the cartoon". Otherwise, print "you can take a walk".

Expected output:

how old are you?
>>> 8
you should watch the cartoon

how old are you?
>>> 15
you can take a walk


{0.5 points} Task 11:
To do: The student received a grade. If it is 2 points, then the program should output "it's very bad"; if it is a 3 - program should print "it's bad"; if it is 4 - "it's good", in case of 5 - "it's excellent", otherwise - "there are no such marks".

Expected output:

what's your grade?
>>> 2

what's your grade?
>>> 4
"it's good"


{0.5 points} Task 12:
To do: The program must request the time of a day in hours (from 1 to 24). Depending on the time entered, display a message indicating what time of a day the entered hour belongs to (midnight (24), night (1-4), morning (5-11), day (12-16), evening (17-13)).

Expected output:

what's time of the day?
>>> 2
"night"

what's time of the day?
>>> 24
"midnight"


{0.5 points} Task 13:
To do: Two integer values are given (set them to variables A and B). Assign a sum of these values to each variable if their values are not equal; otherwise (if they are equal) assign zero to the variables. Output the new values of variables A and B.

Expected output:

please enter two integer numbers
3 5
The result is A = 8, B = 8

please enter two integer numbers
4 4
The result is A = 0, B = 0


{0.5 points} Task 14:
To do: An two-digit integer N is entered, |N| ∈ [10;99].
‒ If it is even and divided by 4, then "add" 4 digit from the left (that is, to form a new number, which in the category of hundreds has 4, and the digits of tens and units are left as in the original number);
‒ If it is even, but is not divided by 4, then "add" 2 digit from the left of the number;
‒ If it is odd, then "add" 0 digit from the right of the number. Print the resulting number.

Expected output:

N:
>>> 16
result =  416

N:
>>> 86
result = 286

N:
>>> 31
result = 310


{0.5 points} Task 15:
To do: Integer number x is entered. Calculate the value of the function f:

Note: The ∪ sign means boolean or.

Expected output:

Enter an integer:
>>> 4
Result is 8


{0.5 points} Task 16:
To do: Real number x is entered. Calculate the value of the function f:

Expected output:

please, enter an x value: -2
result of 6-x =  8

please, enter an x value: 5
result of 2sin(x) =  -1.91784854932628


{0.5 points} Task 17:
To do: A three-digit integer a is entered. Print True if there is a permutation of digits that makes number which is divided by 10, and False otherwise (e.g. 602 => True [620, 260]).

Note. It is allowed to use no more than three operators div and mod (in total).

Expected output:

Enter three-digit integer:
>>> 602
True

Enter three-digit integer:
>>> 311
False

Enter three-digit integer:
>>> -100
True


### Switch statement

{0.3 points} Task 18:
To do: Perform the task using the Case statement. The student received a grade. If it is 2 points, then the program should output "it's very bad"; if it is a 3 - program should print "it's bad"; if it is 4 - "it's good", in case of 5 - "it's excellent", otherwise - "there are no such marks".

Expected output:

what's your grade?
>>> 2

what's your grade?
>>> 4
"it's good"


{0.3 points} Task 19:
To do: Perform the task using the Case statement. An integer in the range [1;7] is entered. Output a name of the day corresponding to this number (1 - “Monday”, 2 - “Tuesday”, etc.).

Expected output:

please enter an integer from 1-7, for day of the week
4
Thursday

please enter an integer from 1-7, for day of the week
9
such day does not exist


{0.3 points} Task 20:
To do: Perform the task using the Case statement. Arithmetic operations are numbered as follows: 1 - addition, 2 - subtraction, 3 - multiplication, 4 - division. Ask user to enter number - arithmetic operation (integer in the range of [1;4]) and two reals A and B (B is not = 0). Output the result of the specified arithmetic operation with the given numbers.

Expected output:

Enter arithmetic operation, please (from 1 till 4):
>>> 1
Enter two real numbers:
>>> 2.2  >>> 5.0
the result is 2.2 + 5.0 = 7.2

Enter arithmetic operation, please (from 1 till 4):
>>> 2
Enter two real numbers:
>>> 5.3  >>> 3.2
the result is 5.3 - 3.2 = 2.5


{0.3 points} Task 21:
To do: Perform the task using the Case statement. Program requests to enter a number of the mass units (1 means kilogram, 2 means ounce, 3 - gram, 4 - ton, 5 - pound). Then the program requests body weight in one of these units. Output body weight in kilograms (1 ounce = 0.0283 kilograms, 1 gram = 0.0010 kilograms, 1 ton = 1000 kilograms, 1 pound = 0.4536 kilograms).

Expected output:

enter body weight
>>> 4
enter mass 1..5
>>> 4
in kilograms = 4000


{0.5 points} Task 22:
To do: Perform the task using the Case statement. The commands are numbered as follows:

1 — check if the number is negative;
2 — check if the number is odd;
3 — check if the number is the divided by entered number D (the number D is entered by the user when this command is selected).

You must ask the user to input an integer N - the sequence number of the C command, and then to output the result. For example, for the command "2" output True if the number is odd and False otherwise.

Note: In response to entering an incorrect command, you should display the message "Command is unknown!".

Expected output:

N :
>>> -23
C :
>>> 1
True

N :
>>> 103
C :
>>> 3
D :
>>> 7
False


### Using a String type

{0.2 points} Task 24:
To do: Perform the task using Case statement: The program must request the time of a day: midnight or night or morning or day or evening. The program must display a range of hours belonging to entered time of a day (midnight (24), night (1-4), morning (5-11), day (12-16), evening (17-23)).

Expected output:

enter the time of the day
>>> night
result: a range of hours for night is 1..4


### Using ranges and enumerations

{0.2 points} Task 25:
To do: Perform the task using Case statement with ranges: The program must request the time of day in hours (from 1 to 24). Depending on the time entered, display a message indicating what time of day the entered hour belongs to (midnight (24), night (1-4), morning (5-11), day (12-16), evening (17-13)).

Expected output:

Enter a time of a day in hours (from 1 to 24), please:
>>> 14
result: 14 hours belongs to day

Enter a time of a day in hours (from 1 to 24), please:
>>> 24
result: 24 hours belongs to midnight


{0.2 points} Task 26:
To do: Perform the task using Case statement with ranges: The program must request number - the age. Depending on the entered number display a message indicating the age of person in words (infancy: from 0 to 1 year old, early childhood: 2-4 years old, preschool: 5-7 years old, school age: 8 - 12 years old, youth: 13-19 years old, second youth: 20–35 years old, adulthood: 36-65 years, old age: over than 66 years).

Expected output:

how old are you?
>>> 6
preschool

how old are you?
>>> 37


Note: tasks should be saved in a file with the name of the task, and be sure to insert a comment with the statement of the task in the code.

CASE

1. Case6. The units of length are numbered as: 1 — decimeter, 2 — kilometer, 3 — meter, 4 — millimeter, 5 — centimeter. The order number N of a unit of length and also the length L of a segment are given (N is an integer in the range 1 to 5, L is a real number). Output the length of the segment in meters.
Expected output:
<< dm =3.15
results:
m = 0.315

2. Case12. Elements of a circle are numbered as: 1 — radius R, 2 — diameter D = 2·R, 3 — length L = 2·π·R of the circumference, 4 — area S = π·R2. The order number of one element and its value (as a real number) are given. Output values of other elements in the same order. Use 3.14 for a value of π.
Expected output:
<< 2 D = 0.79
results:
R = 0.40  L = 2.48  S = 0.49

3. Case15. The suits of playing cards are numbered as: 1 — spades, 2 — clubs, 3 — diamonds, 4 — hearts. Card values "Jack", "Queen", "King", "Ace" are numbered as 11, 12, 13, 14 respectively. A card value N (as an integer in the range 6 to 14) and a suit M (as an integer in the range 1 to 4) are given. Output the card description as: "six of diamonds", "queen of spades", etc.
Expected output:
<<  Card number N = 9,  Card value M = 3
results:
"diamonds nine"


Complete this week's tasks that you didn't have time to do in class. The file names are specified in the tasks. You should upload your files to Microsoft Teams.

## Theory

Lecture # 2 in pdf format

### Integer division and remainder after division

Div
• The div operation is an integer division, where the result is a number without fractional part.
• The div operation calculates the integer part of the result of dividing integers (partial quotient ). For example:
• 10 div  2 = 5
22 div  7 = 3
65 div 10 = 6
---
N div K

The result of N div K shows how many times K «fits inside» N.

Examples:
Example 1.

How many Kilobytes are in x bytes?


Example 2.

x is a time in seconds
How many seconds have passed since the last minute?

Mod
• The mod operation calculates the remainder after dividing two integers. In this case, if the numbers are evenly divisible, the remainder is zero. Example:
• 10 mod  2 = 0
22 mod  7 = 1
65 mod 10 = 5
---
N mod K

The result of N mod K shows the «remainder of N» after the maximum number of K is «thrown» out of N.

Examples
Example 3.

x mod 2 = 0 ⟹ x – even number
x mod 2 <> 0 ⟹ x – odd number


Example 4.

var x := 1234; var LastDigit := x mod 10; // 4 var NumWithoutLastDigit := x div 10; // 123

Example 5.

// x is a 3-digit number. What is the second digit? // Answer: var x := ReadInteger('Enter x:'); // 456 x := x div 10; // 45 Print(x mod 10); // 5

### Bitwise number operation

Formula to get the number out of the quotient, divisor and remainder

By parsing any integer N into two components — the quotient d and remainder m, using the same divisor K and operations div and mod, you will then easily restore this number by the formula:

If we have 10:
10 div 2 = 5
10 mod 2 = 0
↓
5 * 2 + 0 = 10

Examples:

10 div  2 = 5;  10 mod  2 = 0    =>   5*2 + 0  = 10
22 div  7 = 3;  22 mod  7 = 1    =>   3*7 + 1  = 22
65 div 10 = 6;  65 mod 10 = 5    =>   6*10 + 5 = 65

Standard Form Of A Number

Any number can be disassembled into digits using powers of 10 and specified operations. This rule is called the standard form of a number:

123 = 1*100 + 2*10 + 3

Therefore, to make a certain number a hundred, you need to multiply it by 100 (as we have for digit 1 we have 1 * 100 = 100).

3 Rules To Get Digits Of Three-Digit Integer (three bit width)
It can be seen that:

• to get hundred (first digit) of three-digit number, you need to calculate the quotient after dividing that number by 100 (number div 100, e.g. 123 div 100 = 1)
• to get ten (second digit), you need to calculate the remainder after dividing that number by 100, and then — the quotient of dividing the result by 10 (1. number mod 100: 123 mod 100 = 23; 2. 23 div 10 = 2) (also there is another way)
• to get unit number (third digit), you need to calculate remainder after dividing this number by 10 (number mod 10: 123 mod 10 = 3).
For numbers with a different bit width, these algorithms can be changed.

{0.2} Task 1:
To do: A distance L in centimeters is entered. Use the operation of integer division to convert it to meters of (1 meter = 100 centimeters). Use comments to make the program clear to user. Give the log of your program in the form of a comment after the program code.

Expected output:

Please enter the distance in centimeters
>>> 245
The distance in meters is  2.45


{0.2} Task 2:
To do: A mass in kilograms is entered. Use integer division to convert it to tons (1 ton = 1000 kilos). Use comments to make the program understandable to user. Give the log of your program as a comment after the program code.

Expected output:

Please enter the mass in kilos
>>> 4527
The mass in tons is 4


{0.3} Task 3:
To do: The person’s age in months is entered. Convert it to a person’s age in years (e.g: 65 months is 5 full years, 24 months is 2 years). Verify the correctness of your program, give the log as a comment.

Expected output:

Please enter the age in months
>>> 37
The age in years is 3


{0.3} Task 4:
To do: A two-digit integer is entered. Output its first and second digits separated by commas (i.e. left and right digits).

Note. Run the program and enter a non-two-digit number. What has happened? Is the result correct? Later we will learn how to perform validation of the input data.

Expected output:

Please enter a two digit number
>>> 37
The first and second are  7,  3
+++
Please enter a two digit number:
>>> -35
The first and second are 5, 3
+++
Please enter a two digit number:
>>> 90
The first and second are 0, 9


{0.3} Task 5:
To do: A two-digit integer is entered. Output the addition and multiplication of its digits. Check the correctness of your program.

Note. Each digit of the number will be needed twice: in the calculation of the sum — the first time and in the calculation of the multiplication — the second time. It is recommended to use auxíliary variables for storing the values of the digits.

Expected output:

please enter a two digit number
>>> 73
you entered 73. The result is 10, 21


{0.3} Task 6:
To do: A three-digit integer is entered. Output all of its digits (the order does not matter). Check the correctness of your program, give the log in the form of a comment.

Expected output:

Please enter a three-digit number
>>> -105
The three digits are : 1, 0, 5


{0.3} Task 7:
To do: A three-digit integer is entered. Output the addition of its digits. Make sure that your program works correctly with negative numbers.

Expected output:

Please enter a three-digit number
>>> -745
You entered: 745. The addition of its three digits is equal to 16



{0.4} Task 8:
To do: Two digits (in the range from 0 to 9) are entered. Use the standard form of a number to make a number, the digits of which are the specified digits.

Expected output:

Please enter two digits
>>> 3
>>> 5
The new number is 35


Check the following:

3, 5 => 35
7, 0 => 70
0, 4 => 4


{0.4} Task 9:
To do: A two-digit integer is entered. Swap the values of its left and right digits and output the resulting number. Check the correctness of your program, give the log in the form of a comment.

Expected output:

please enter a two digit number
>>> -57
you entered -57. The result is -75


Check the following:

35 =>  53
-10 => -1


{0.4} Task 10:
To do: A three-digit integer is entered. Swap the values of its left and middle digits. Check the correctness of your program, give the log in the form of a comment.

Expected output:

please enter a three digit integer
>>> 846
After swaping the left and middle digits, the result is 486


{0.4} Task 11:
To do: A three-digit integer is entered. Make a cyclic shift of digits to the left.

Expected output:

please enter a three digit integer
>>> -734
After making a cyclic shift of digits to the left, the result is -347


Check the following:

 123 =>  231
-602 => -26


{0.4} Task 12:
To do: A three-digit integer is known. Make a cyclic shift of digits to the right.

Expected output:

please enter a three digit integer
>>> -184
After a cyclic shift of digits to the right, the result is -418


Note: tasks should be saved in a file with the name of the task, and be sure to insert a comment with the statement of the task in the code.

INTEGER

1. Integer7. A two-digit integer is given. Find the sum and the product of its digits.
Expected output:
<< 47
results:
sum = 11  product = 28

2. Integer9. A three-digit integer is given. Using one operator of integer division find first digit of the given integer (a hundreds digit).
Expected output:
<< 188
results:
1

3. Integer10. A three-digit integer is given. Output its last digit (a ones digit) and then its middle digit (a tens digit).
Expected output:
<< 916
results:
ones 6, tens 1

4. Integer11. A three-digit integer is given. Find the sum and the product of its digits.
Expected output:
<< 363
results:
sum 12, product 54

5. Integer12. A three-digit integer is given. Output an integer obtained from the given one by reading it from right to left.
Expected output:
<< 263
results:
362

6. Integer13. A three-digit integer is given. Output an integer obtained from the given one by moving its left digit to the right side.
Expected output:
<< 124
results:
241


## Theory

Lecture # 1 in pdf format

### Variable Definition & Assigning a value to it

• In Pascal abc.net variables can be defined within the body of the program between begin and end keywords. The principle of locality: a variable is defined immediately before it is used.
• When we define a variable, we specify its name and type:

•
So we have two possible versions:

• var n: integer; // variable declaration begin n:=1; // assignment statement
• pascalAbc.net:
• 1 method:

begin var n:integer; // variable declaration n:=1; // assignment statement

2 method (canonical method when type is defined depending on the value):

begin var n:=1; // variable declaration and assignment statement => initialization

### Arithmetic operations and expressions

common method:

begin var a := 6; // Assigning value 6 a:= a + 2; // Increasing by 2 a:= a - 2; // substraction of 2 a:= a * 3; // Multiplication by 3 a:= a / 2; // division end.

short method:

begin var a := 6; // Assigning value 6 a+= 2; // Increasing by 2 a-= 2; // substraction of 2 a*= 3; // Multiplication by 3 a/= 2; // division end.

### Data input

1-st way:

begin var n:integer; // n is a variable of integer type read(n); // input some value to store it in n variable
begin var n:real; // n is a variable of real type - floating point number read(n);// input some value to store it in n variable

2-nd way:

var n:=ReadInteger(); // n is a variable of integer type & we input some value to store it in n var x:=ReadReal(); // x is a variable of real type & we input some value to store it in x

3-d way (tuple assignment):

var n1, n2: integer; // two integers are declared (n1, n2) := (1, 2); // 1 is assigned to n1, 2 is assigned to n2

4-th way:

var(n1, n2) := readInteger2; // n1 and n2 are the variables of int type & we input some values to store it in them

Usually before data reading you must print the prompt with an explanation of what data you read:

var x := ReadInteger('Enter x:'); var y := ReadInteger('Enter y:'); var res := x + y;

### Data output

1-st way:

begin var n:integer; read(n); n: = n * n; writeln('n = ',n);

2-nd way:

begin begin var n:integer; read(n); n: = n * n; print('n = ',n);

What does formatted output mean?

For beautiful output, you should use formatted output with the WritelnFormat procedure or Print:
1. WritelnFormat:

begin var a:=1.2; var b:=4; var c:=a+b; WritelnFormat ('f ({0}, {1}) = {2}', a, b, c); end.

The result will be:

f (1.2, 4) = 5.2

The first parameter in brackets and single quotes is a format string that specifies the format for outputting expressions.
So, if we want to output:

a + b = b + a = sum

then you just need to replace a, b with {0}, {1}:

WritelnFormat ('{0} + {1} = {1} + {0} = {2}', a, b, x + y)
• You can specify the width (W) of the output field of the expression N (width in characters): {N,W}. For example, the operator
• WritelnFormat('x = *{0,5}*', x); // 5 means 5 charecters for displaying x

works this way:

x = *    6*
x = *   -3*
x = *  123*
x = *-9876*


2. Print:

var x := ReadInteger('Enter x:'); var y := ReadInteger('Enter y:'); var res := x + y; Print($'Sum of {x} and {y} is {res}'); ### Swapping Variable Values We have: var (x,y) := (3,5); To do: To swap values of variables: Solution 1. Using temporary variable: var t := x; x := y; y := t; Solution 2. Using multiple assignment: (x,y) := (y,x); Two assignments x := y and y := x are carried out simultaneously! Not sequentially! ## Labs and tasks ### Follow the rules to make the tasks 1. Save your files with names as it is given in tasks (e.g. task-04.pas). 2. Give meaningful names to your variables. 3. Use comments to make the program clear. 4. Give the task of the program as a comment before the program code. Use curly braces for comments: 5. Give the results of your program (log) as a comment after the program code. It’s easy to do just by copying. Use curly brackets to add comments: Sample 1: To do: Calculate the expression. The values of x, y and z are entered. Expected output: Input x 3 Input y 4 Input z 5 result = 1.77800712886037  [Program name: L1sample1.pas] ✍ Algorithm (how to do): {0.2} Task 1: To do: Calculate an average of two variables a and b (formula for calculation: a + b)/2). Values of variables are provided (a=5, b=6). You should do this task twice with different ways of assigning and output. Note: it is better to use here formatted output. Expected output: (5 + 6) / 2 = 5.5  [Program name: L1task00.pas and L1task01.pas] {0.2} Task 2: To do: Assign the values to two variables (a=-0.80, b=-8.0). Calculate the sum, substruction, product and quotient of the variables’ squares. Note 1: To specify a particular number of digits after the floating point you can use format expression of writeln function: writeln('result = ', x:5:3) 5 means total number of signs to output the number, 3 means the number of digits to output after floating point.  Note 2: it is better to use here formatted output. Expected output: a^2 + b^2 = 64.64 a^2 * b^2 = 40.96 a^2 - b^2 = -63.36 a^2 / b^2 = 0.01  [Program name: L1task02.pas] Sample 2: To do: The side of a square (variable name is side) is entered. Calculate its perimeter: P = 4·a. Use different methods of assigning, input and output. Expected output: please enter the side length of a square: 5.6 Perimeter P = 22.4  [Program name: L1sample2.pas] ✍ Algorithm (how to do): 1-st way: begin // Variable declaration to store the value of the side length var a := ReadReal('please enter the side length of a square:'); var p := 4 * a; // Perimeter calculation Print($'Perimeter P = {p}'); end.

2-nd way:

begin PrintLn('please enter a side length of a square:'); // Variable declaration to store the value of the side length var a: real; readln(a); var p := 4 * a; // Perimeter calculation Print('Perimeter P = ', p); end.
{0.3} Task 3:

To do: The side of the square (variable name is side) is entered. Calculate an area of the square: S = a2. You should use different methods of assigning, input and output.

Note: To calculate square of a number you can use sqr() standart function, for example:

sqrX:=sqr(x);

Expected output:

enter a side length of a square:
2.90
Area S = 8.41


{0.3} Task 4:

To do: The sides of the rectangle are entered (a and b). Calculate an area of the rectangle (S = a*b) and its perimeter (P = 2 (a + b)).

Note: To specify a particular number of digits after the floating point you can use format expression of writeln function:

writeln('S = ', S:0:2); // :2 means the number of digits to output after floating point

Expected output:

Enter the values of two sides:
12
13
result:
S = 156.00
P = 50.00


{0.4} Task 5:

To do: A diameter of a circle (variable name is d) is entered. Calculate its length (formula L = π·d). The value of π is 3.14. Use different methods of assigning, input and output.

Note 1: π has a constant value. In pascalAbc we can declare constant before the begin section of the program:

const pi = 3.14; begin // ... end.

Note 2: Make the program using the same style of coding as in sample 2.

Expected output:

please enter a diameter of a circle:
6.7
the length of a circle is: 21.038


Sample 3:

To do: Calculate hypotenuse and perimeter of a right-angled triangle; legs of the triangle are entered (square root of (a2 + b2)).

Note: To calculate square root of a number you can use sqrt() standart function, for example:

sqrtX:=sqrt(x);

Expected output:

Input the values of triangle legs:
3.0
6.0
hypotenuse = 6.70820393249937
perimeter = 15.7082039324994


[Program name: L1sample3.pas]

✍ Algorithm:

Here is an example of right program which is clear for user:
{0.4} Task 6:

To do: A length of a cube side is entered (a). Calculate a volume of the cube (V = a3) and its surface area (S = 6·a2). Give the program log in the form of a comment.

Note: To specify a particular number of digits after the floating point you can use format expression of writeln function:

writeln('V = ', v:5:3)
5 means total number of signs to output the number,
3 means the number of digits to output after floating point.


Expected output:

enter a cube side length:
9.000
V = 729.000
S = 486.000


{0.4} Task 7:

To do: Assign a value to integer variable x (x = 5). Calculate the value of the function:

y = 4(x-3)6 - 7(x-3)3 + 2


Note 1: To calculate the power of a number you can use the power(x:real, y:real) function. For example:

//2 in the power of 5 = powNumb = power (2,5);

Note 2: It is better to use an auxiliary variable for (x-3)3.

Expected output:

for x = 5 we have y = 202


{0.4} Task 8:

To do: Calculate a distance between two points with the given coordinates x1 and x2 on the number axis; the coordinates are entered. The formula is |x2 − x1|.

Note: To calculate the absolute value of a number you can use abc(x:real) standart function:

abs(x2 - x1);

Expected output:

x1 = 3.2 x2 = 2.5 the distance between two points: 0.7

{0.4} Task 9:

To do: Calculate a distance between two points on the plane; coordinates (x1,y1) and (x2,y2) are entered. The distance is calculated by the formula:

Note 1: Verify the correction of your program by using «simple» values that are easy to calculate. For example:

d((0,  0); (6, 0)) = 6;
d((0,  -4); (0, 1)) = 5;
d((-1,  1); (2, 5)) = 5:


Note 2: Display the results of your program (log) in the form of a comment after the program code. It’s easy to do by copying and pasting. You should use curly brackets for comments:

Expected output:

enter x1 of the first point: 0 enter y1 of the first point: 0 enter x2 of the second point: 6 enter y2 of the second point: 0 The distance equals 6

{0.3} Task 10:

To do: The temperature in Celsius is entered, convert temperature to Fahrenheit. Celsius and Fahrenheit scales are related by the ratio:

and opposite:

Expected output:

enter the temperature in Celsius 56 The temperature in Fahrenheit 132.8

### Swapping Variable Values

{0.2} Task 11:

To do: Swap the values of variables A and B and print out the new values to the console.

Expected output:

Enter A: 5.7 Enter B: 3 Result: A = 3, B = 5.7

{0.2} Task 12:

To do: The values of variables A, B, C are entered. Swap their values to make A = B, B = C, C = A, and display the results.

Expected output:

A = 3.4 B = 2 C = 1.5 Result: A = 1.5, B = 3.4, C = 2

Note: tasks should be saved in a file with the name of the task, and be sure to insert a comment with the statement of the task in the code.

BEGIN

1. Begin12. The legs a and b of a right triangle are given. Find the hypotenuse c and the perimeter P of the triangle:
c = (a2 + b2)1/2,
P = a + b + c.

Expected output:
<< a=4.90
<< b=9.90
results:
c=11.05
P=25.85

2. Begin17. Three points A, B, C are given on the real axis. Find the length of AC, the length of BC, and the sum of these lengths.
Expected output:
<< A=-3.80
<< B=3.40
<< C=0.50
results:
AC=4.30
BC=2.90
AC+BC=7.20

3. Begin23. Variables A, B, C are given. Change values of the variables by moving the given value of A into the variable B, the given value of B into the variable C, and the given value of C into the variable A. Output the new values of A, B, C.
Expected output:
<< A=2.47
<< B=1.41
<< C=9.50
results:
A=9.50
B=2.47
C=1.41

4. Begin27. Given a number A, compute a power A8 using three multiplying operators for computing A2, A4, A8 sequentially. Output all obtained powers of the number A.
Expected output:
<< A=3.20
results:
A2=10.24  A4=104.86  A8=1095.12

5. Begin28. Given a number A, compute a power A15 using five multiplying operators for computing A2, A3, A5, A10, A15 sequentially. Output all obtained powers of the number A.
Expected output:
<< A=1.57
results:
A2=2.46  A3=3.87  A5=9.54  A10=90.99  A15=867.95

6. Begin40. Solve a system of linear equations
A1·x + B1·y = C1,
A2·x + B2·y = C2


with given coefficients A1, B1, C1, A2, B2, C2 provided that the system has the only solution. Use the following formulas:

x = (C1·B2 − C2·B1)/D,
y = (A1·C2 − A2·C1)/D,
where D = A1·B2 − A2·B1.

Expected output:
<< A1=-3.00  << B1=-2.00  << C1=4.00
<< A2=-1.00  << B2=-4.00  << C2=-2.00
results:
x = -2.00  y = 1.00

Вставить формулу как
Дополнительные настройки
Цвет формулы
Используйте LaTeX для набора формулы
Предпросмотр
$${}$$
Формула не набрана
Вставить