Last Updated:

Pascal Programming - Basic Example Codes [1]

Pascal  Cycles - Pascal Programming

The user enters N integers. The program calculates the product of positive and negative numbers.

Decision:

After entering N positive integers, the program calculates the sum and number of multiples of three.

Decision:

Make a program for calculating the values of the function f = 7 * sin(x^2) -0.5 * cosx on the segment [a; b] with steps h. The result is presented in the form of a table, the first column of which is the values of the argument, the second - the corresponding values of the function.

Decision:

 

Write a program that displays two columns, in the first - an integer from 1 to 10, and next to it in the second its square.

Decision:

Make a program that calculates the arithmetic mean of N numbers entered from the keyboard.

Decision:

Pascal Files - Pascal Programming

 

Write a program that injects n items into the file and then outputs its contents.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
program file_1;
uses crt;
var
n, i, x: integer;
f: file of integer;
begin
clrscr;
write('Number of numbers >')read(n);
assign(f, ‘fail’);
rewrite(f);
for i:=1 to n do
begin
write(i, ' element > ')read(x);
write(f, x);
end;
writeln('File Contents:');
reset(f);
for i:=1 to n do
begin
read(f, x);
write(x, ‘ ‘);
end;
close(f);
readkey;
end.
The nth number of lines are entered into the file. Display the smallest and largest row.
 

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
program file_2;
uses crt;
var
n, i: integer;
s, smin, smax: string;
f: text;
begin
clrscr;
write('Number of lines > ')read(n);
assign(f, ‘fail.txt’);
rewrite(f);
for i:=1 to n do
begin
write(i, ' string > ')read(s);
writeln(f, s);
end;
smin:=s; smax:=s;
reset(f);
while not eof(f) do
begin
readln(f, s);
if length(smin)>length(s) then smin:=s;
if length(smax)<length(s) then smax:=s;
end;
writeln('Smallest string: ', smin);
writeln('Largest string: ', smax);
close(f);
readkey;
end.

A text file consisting of N lines is given. Organize the replacement of symbols in the file. "Old" and "new" characters are entered from the keyboard.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
program file_3;
uses crt;
var
n, i: integer;
s: string;
c1, c2: char;
f1, f2: text;
begin
clrscr;
assign(f1, ‘fail1.txt’);
assign(f2, ‘fail2.txt’);
rewrite(f1);
write('Number of lines > ')read(n);
for i:=1 to n do
begin
write(i, ' string > ')read(s);
writeln(f1, s);
end;
reset(f1); rewrite(f2);
write('Remove the >') characterread(c1);
write('Insert >')read(c2);
while not eof(f1) do
begin
readln(f1, s);
for i:=1 to length(s) do
if s[i]=c1 then
write(f2, c2)
else write(f2, s[i);
writeln;
end;
reset(f2);
writeln('Result:');
while not eof(f2) do
begin
readln(f2, s);
writeln(s)
end;
close(f1); close(f2);
readkey;
end.

Write random integers to the first file. Fill the second file with numbers that are the product of neighboring components of the first file.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
program file_4;
uses crt;
var
n, i, x, y: integer;
f, g: file of integer;
begin
clrscr; randomize;
assign(f, ‘fail_f’);
assign(g, ‘fail_g’);
rewrite(f);
write('Number of numbers >')read(n);
for i:=1 to n do
begin
x:=random(10);
write(f, x);
write(x, ‘ ‘);
end;
reset(f); rewrite(g);
writeln;
while filepos(f)<n1 do
begin
read(f, x);
read(f, y);
write(g, y*x);
seek(f, filepos(f)1);
end;
reset(g);
writeln('Result:');
while not eof(g) do
begin
read(g, y);
write(‘ ‘, y)
end;
close(f); close(g);
readkey;
end.

Create a test of 5 questions on the topic of YP Pascal. Questions should be displayed on the screen from the file one at a time. At the end, the tester receives the number of questions to which he answered correctly.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
program file_5;
uses crt;
var
i, k: integer;
s, x: string;
t_file, o_file: text;
begin
clrscr;
assign(t_file, ‘test.txt’);
assign(o_file, ‘otvet.txt’);
reset(t_file); reset(o_file);
k:=0;
while not eof(t_file) do
begin
for i:=1 to 6 do
begin
readln(t_file, s);
writeln(s);
end;
readln(o_file, s);
write('Reply > ')read(x);
if x=then k:=k+1;
clrscr;
end;
write('correct answers: ', k);
close(t_file); close(o_file);
readkey;
end.

A file is given, with the text pre-recorded in it. Check if the parentheses are placed correctly in it.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
program file_6;
uses crt;
var
i, k, c: integer;
s: string;
f: text;
begin
clrscr;
assign(f, ‘file.txt’);
reset(f);
k:=0; c:=0;
while not eof(f) do
begin
read(f, s[i]);
if s[i]=‘(‘ then c:=c+1
else if s[i]=‘)’ then k:=k+1;
end;
if (k=c) and ((k+c) mod 2=0) then
write('Brackets are placed correctly')
else write('Brackets are not placed correctly');
close(f);
readkey;
end.

Populate file A with integers obtained using a random number generator. Get in file B those components of file A that have an even value.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
program file_7;
uses crt;
var
n, i, x: integer;
A, B: file of integer;
begin
clrscr; randomize;
assign(A, ‘fail_A’);
assign(B, ‘fail_B’);
rewrite(A); rewrite(B);
write('Number of numbers >')read(n);
write('File A content: ');
for i:=1 to n do
begin
x:=random(10);
write(A, x);
write(‘ ‘, x);
end;
writeln;
rewrite(B); reset(A);
write('File B content: ');
while not eof(A) do
begin
read(A, x);
if x mod 2=0 then
begin
write(B, x);
write(‘ ‘, x);
end;
end;
close(A); close(B);
readkey;
end.

Make a program that writes numbers from file A to file B without repetition. For example, from sequence 1, 3, 5, 3 in B, there will be 1, 3, 5.

 

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
program file_8;
uses crt;
var
n, i, j, x: integer;
mas: array[1.. 100] of integer;
v: boolean;
A, B: file of integer;
begin
clrscr; randomize;
assign(A, ‘fail_A’);
assign(B, ‘fail_B’);
rewrite(A); rewrite(B);
write('Number of numbers >')read(n);
write('Contents of file A:');
for i:=1 to n do
begin
mas[i]:=random(10);
write(A, mas[i]);
write(‘ ‘, mas[i]);
end;
writeln;
reset(A); rewrite(B);
write('Contents of file B:');
j:=1;
while not eof(A) do
begin
read(A, x);
v:=true;
j:=j+1;
for i:=to n do
begin
if x=mas[i] then v:=false;;
end;
if v then
begin
write(B, x);
write(‘ ‘, x);
end;
end;
close(A); close(B);
readkey;
end.
A text file that has n lines is filled in from the keyboard. Delete all numbers in it.
 

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
program file_9;
uses crt;
var
n, i: integer;
s: string;
f1, f2: text;
begin
clrscr; randomize;
assign(f1, ‘fail1’);
assign(f2, ‘fail2’);
rewrite(f1); rewrite(f2);
write('Number of lines > ')read(n);
for i:=1 to n do
begin
write(i, ' string > ')read(s);
writeln(f1, s)writeln(f2, s);
end;
reset(f1); rewrite(f2);
while not eof(f1) do
begin
readln(f1, s);
i:=1;
while i<=length(s) do
begin
if (ord(s[i])>47) and (ord(s[i])<58then
begin
delete(s, i, 1);
i:=i1;
end;
i:=i+1;
end;
writeln(f2, s);
end;
reset(f2);
writeln('Result:');
while not eof(f2) do
begin
readln(f2, s)writeln(s);
end;
close(f1); close(f2);
readkey;
end.

The program counts the number of x characters (entered by the user) in the file.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
program file_10;
uses crt;
var
n, i, k: integer;
s: string;
c: char;
f: text;
begin
clrscr; randomize;
assign(f, ‘fail’);
rewrite(f);
write('Number of lines > ')read(n);
for i:=1 to n do
begin
write(i, ' string > ')read(s);
writeln(f, s);
end;
write('symbol > ')read(c);
reset(f);
i:=1; k:=0;
while not eof(f) do
begin
read(f, s[i]);
if (s[i]=c) then k:=k+1;
i:=i+1;
end;
reset(f);
writeln('Result:');
write('Character ', c, ' occurs ', k, ' raz(a)');;
close(f);
readkey;
end.

File A is filled with random numbers. Form file B from file elements A multiples of m, and file C from file elements A multiples of h. m and h are entered from the keyboard.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
program file_11;
uses crt;
var
n, i, x, m, h: integer;
a, b, c: file of integer;
begin
clrscr; randomize;
assign(a, ‘faila’);
assign(b, ‘failb’);
assign(c, ‘failc’);
rewrite(a);
write('Number of elements > ')read(n);
writeln('File A:');
for i:=1 to n do
begin
x:=random(20)10write(a, x);
write(x, ‘ ‘);
endwriteln;
write(‘M > ‘)read(m);
write(‘H > ‘)read(h);
reset(a); rewrite(b); rewrite(c);
while not eof(a) do
begin
read(a, x);
if (mod m=0) and (mod h=0) then
begin
write(b, x);
write(c, x);
end
else if x mod m=0 then write(b, x)
else if x mod h=0 then write(c, x)
end ;
close(a); reset(b); reset(c)writeln;
write('File B(x/', m, '):');
while not eof(b) do
begin
read(b, x)write (‘ ‘, x);
endwriteln;
write('File C(x/', h, '):');
while not eof(c) do
begin
read(c, x)write(‘ ‘, x);
end;
close(b); close(c);
readkey;
end.

Write n real numbers to the file. Find the maximum among them.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
program file_12;
uses crt;
var
i, n: integer;
x, max: real;
f: file of real;
begin
clrscr;
assign(f, ‘file’);
write('Number of numbers >')read(n);
rewrite(f);
for i:=1 to n do
begin
write(i, ' number > ')read(x)write(f, x);
if i=1 then max:=x;
end;
reset(f);
i:=1;
while not eof(f) do
begin
read(f, x);
if (mod 2<>0) then
begin if x>max then max:=x; end;
i:=i+1;
end;
write('Odd max: ', max:6:3);
close(f);
readkey;
end.

Integers are entered into the file from the keyboard. Calculate and display the difference between the first and last elements, as well as their product.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
program file_13;
uses crt;
var
x, i, n, first, last: integer;
f: file of integer;
begin
clrscr;
assign(f, ‘file’);
write('Number of numbers >')read(n);
rewrite(f);
for i:=1 to n do
begin
write(i, ' number > ')read(x)write(f, x);
end;
reset(f);
i:=1;
while not eof(f) do
begin
read(f, x);
if i=1 then first:=x
else if i=then last:=x;
i:=i+1;
end;
writeln(first, ‘-‘, last, ‘=’, firstlast);
write(first, ‘*’, last, ‘=’, last*first);
close(f);
readkey;
end.

Calculate how many empty lines are in a pre-created file.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
program file_14;
uses crt;
var
i, n, k: integer;
s: string;
f: text;
begin
clrscr;
assign(f, ‘file.txt’);
{ File is created in advance }
reset(f);
k:=0;
while not eof(f) do
begin
readln(f, s);
if length(s)=0 then k:=k+1;
i:=i+1;
end;
write('In file ', k, 'empty line(s)');
close(f);
readkey;
end.

Pascal Functions. - Pascal Programming

 

Write a function that returns the larger of the two real numbers obtained as an argument.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
program functions_1;
uses crt;
var
a, b: real;
function Max(a, b: real): real;
begin
if a>b then Max:=a
else Max:=b
end;
begin
clrscr;
write(‘A > ‘)read(a);
write(‘B > ‘)read(b);
write(Max(a, b),' is a larger number');
readke

The function calculates the sum of integers that do not exceed the user-defined number N.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
program functions_2;
uses crt;
var
n: integer;
function Sum(m: integer): integer;
var i, S: integer;
begin
S:=0;
for i:=1 to m do
S:=S+i;
Sum:=S;
end;
begin
clrscr;
write(‘N > ‘)read(n);
write('Sum of numbers from 1 to ', n, ' = ', Sum(n)));
readkey;
end.

Make a program whose function raises the number a to the power of b, where a is any number and b is any integer.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
program functions_3;
uses crt;
var
a: real;
b: integer;
function S(sa: real; sb: integer): real;
var i: integer; x: real;
begin
x:=sa;
if sb=0 then S:=1
else if sb<0 then
begin
for i:=2 to abs(sb) do sa:=sa*x;
S:=1/sa;
end
else
begin
for i:=2 to sb do sa:=sa*x;
S:=sa;
end
end;
begin
clrscr;
write(‘A > ‘)read(a);
write(‘B > ‘)read(b);
write(a, ‘^’, b, ‘ = ‘, S(a, b));
readkey;
end.

The program calculates the factorial of the positive number N.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
program functions_4;
uses crt;
var
n: integer;
function factorial(m: integer): integer;
var i, f: integer;
begin
f:=1;
for i:=1 to m do
f:=f*i;
factorial:=f;
end;
begin
clrscr;
write(‘N > ‘)read(n);
write(n, ‘! = ‘, factorial(n));
readkey;
end.

The real number A is given. Calculate the percentage B of this number.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
program functions_5;
uses crt;
var
a, b: real;
function percent(pa, pb: real): real;
var p: real;
begin
p:=pa/100*pb;
percent:=p;
end;
begin
clrscr;
write('Number > ')read(a);
write('Percentage > ')read(b);
write(b, '% of ', a, ' = ', percent(a, b)));
readkey;
end.

Write a function that calculates the number of spaces in a entered line.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
program functions_6;
uses crt;
var
s: string;
function space(ss: string): integer;
var i, p: integer;
begin
p:=0;
for i:=1 to length(s) do
if ord(s[i])=32 then p:=p+1;
space:=p;
end;
begin
clrscr;
write('String > ')read(s);
write('String contains ', space(s),' 'space(a/ov)');
readkey;
end.
 

An Armstrong number is a natural number that is equal to the sum of its digits raised to a power equal to the number of its digits. Find all such numbers from 1 to n.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
program functions_7;
uses crt;
var
n, i, y: integer;
function Armstrong(ai: integer): integer;
var j, a, b, c, f, d: integer;
begin
a:=0; b:=0; f:=ai; d:=ai;
while ai>0 do
begin
ai:=ai div 10;
b:=b+1;
end;
ai:=f; f:=0;
while ai>0 do
begin
a:=ai mod 10;
ai:=ai div 10;
c:=a;
for j:=2 to b do a:=a*c;
f:=f+a;
end;
if f=then Armstrong:=f
else Armstrong:=0;
end;
begin
clrscr;
write(‘N > ‘)read(n);
writeln('Armstrong Numbers:');
for i:=1 to n do
begin
y:=Armstrong(i);
if y<>0 then write(y, ' ');
end;
readkey;
end.

Pascal Procedures - Pascal Programming

 

Write a function to exchange the values of two variables

Decision:

The program displays the number of stars specified by the user.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
program procedures_2;
uses crt;
var
z: integer;
procedure zvezd(n: integer);
var i: integer;
begin
for i:=1 to n do
write(‘*’);
end;
begin
clrscr;
write('Number of asterisks >')read(z);
zvezd(z);
readkey;
end.

The matrix is given. Get a vector from it that contains the sums of the row elements. Sort it in descending order of elements.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
program procedures_3;
uses crt;
var
rez, k, m: integer;
y: array[1.. 5, 1.. 5] of integer;
mas: array[1.. 5] of integer;
{procedure}
procedure matr_sum(n, r: integervar A: array[1.. 5] of integer);
var i, j, x, key: integer;
begin
A[n]:=A[n]+r;
if n=5 then
begin
for i:=1 to 5 do
begin
x:=A[i];
key:=i;
for j:=i+1 to 5 do
if A[key]<A[j] then
key:=j;
if key<>i then
begin
A[i]:=A[key];
A[key]:=x;
end;
end;
end;
end;
{main program}
begin
clrscr;
randomize;
{matrix processing}
for k:=1 to 5 do
begin
rez:=0;
for m:=1 to 5 do
begin
y[k, m]:=random(10){assign random values to elements}
write(y[,m]:3);
rez:=rez+y[k, m];
end;
matr_sum(k, rez, mas){procedure call}
writeln;
end;
writeln('New Array');
for k:=1 to 5 do {output of the resulting vector}
write(mas[k]:3);
readkey;
end.

Write a procedure that calculates the sum of the elements in an array that have an even value and an odd value.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
program procedures_4;
uses crt;
type Arr=array[1.. 100] of integer;
var
A: Arr;
i, n, sum1, sum2: integer;
procedure sum(d: integer; mas: Arr);
var j, sum1, sum2: integer;
begin
for j:=1 to d do
if mas[j] mod 2=0 then sum1:=sum1+mas[j]
else sum2:=sum2+mas[j];
writeln;
writeln('Sum of even elements: ', sum1);
write('Sum of odd elements: ', sum2);
end;
begin
clrscr;
write('Number of elements > ')read(n);
for i:=1 to n do
begin
A[i]:=random(10);
write(A[i], ‘ ‘);
end;
sum1:=0; sum2:=0;
sum(n, A);
readkey;
end.

Use the procedure to display characters, the type and quantity of which are specified from the keyboard.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
program procedures_5;
uses crt;
var
n: integer;
c: char;
procedure symbol(fn: integer; fc: char);
var i: integer;
begin
for i:=1 to fn do
write(fc);
end;
begin
clrscr;
write('Symbol > ')read(c);
write('Number of characters > ')read(n);
symbol(n, c);
readkey;
end.

The program raises the number a to the power of b, where a is any number and b is any integer.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
program procedures_6;
uses crt;
var
a, rez: real;
b: integer;
procedure power(pa: real; pb:integervar prez: real);
var p: real; i: integer;
begin
p:=pa;
if pb>0 then
for i:=pb downto 2 do
begin
pa:=pa*p;
prez:=pa;
end
else for i:=abs(pb) downto 2 do
begin
pa:=pa*p;
prez:=1.00/pa;
end;
end;
begin
clrscr;
write('Number > ')read(a);
write('Degree >')read(b);
if b=1 then write(a, ‘^’,b, ‘=’, a)
else if b=0 then write(a, ‘^’,b, ‘=1’ )
else
begin
power(a, b, rez);
write(a, ‘^’,b, ‘=’,rez);
end;
readkey;
end.
 

An array of random numbers from -30 to 33 is given. Double all positive elements.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
program procedures_7;
uses crt;
type Arr=array[1.. 100] of integer;
var
A: Arr;
i, n: integer;
procedure Massiv(d: integer; mas: Arr);
var j: integer;
begin
for j:=1 to d do
begin
if mas[j]>=0 then mas[j]:=mas[j]*2;
write(mas[j]:4);
end;
end;
begin
clrscr;
write('Number of elements > ')read(n);
for i:=1 to n do
begin
A[i]:=random(64)30;
write(A[i]:4);
end;
writeln;
Massiv(n, A);
readkey;
end.

Characters and strings - Pascal Programming

 

The program asks for the username and says hello to it.

Decision:

1
2
3
4
5
6
7
8
9
10
program characters_and_strings_1;
uses crt;
var s: string[25];
begin
clrscr;
write('Your name > ');
read(s);
write('Hello, ', s, '!' );
readkey;
end.

Given a line. Display it character-by-character, so that there must be some time delay between the output of the symbol i and i+1. To do this, use the Delay procedure(<time>).

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
program characters_and_strings_2;
uses crt;
var s: string;
i: integer;
begin
clrscr;
write('String > ')read(s);
for i:=1 to length(s) do
begin
delay(80);
write(s[i]);
end;
readkey;
end.
 

Count how many words are in a line that ends with a period.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
program characters_and_strings_3;
uses crt;
var s: string;
i, c: integer;
begin
clrscr;
write('String > ')read(s);
c:=1;
for i:=1 to length(s) do
if s[i]=‘ ‘ then c:=c+1;
write('In the string ', c, ' words(a)');
readkey;
end.
 

The user enters a character, and receives its ASCII code.

Decision:

1
2
3
4
5
6
7
8
9
10
program characters_and_strings_4;
uses crt;
var
c: char;
begin
clrscr;
write('symbol > ')read(c);
write('ASCII character code', c, ': ', ord(c)';
readkey;
end.
 

The program outputs 128 characters of the ASCII table. It should have 16 rows and 8 columns, and the characters were displayed in columns from top to bottom.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
program characters_and_strings_5;
uses crt;
var
s: array[1.. 16, 1.. 8] of char; ch: char ;
i, j, n, x: integer;
mas: array[1.. 16, 1.. 8] of integer;
arr: array[1.. 128] of integer;
begin
x:=0;
for i:=1 to 8 do
for j:=1 to 16 do
begin
mas[j, i]:=x;
s[j, i]:=char(x);
x:=x+1;
end;
{table output}
for i:=1 to 16 do
begin
for j:=1 to 8 do
begin
if mas[,j]=10 then {for correct output of the 10th character}
write(mas[i, j]:6, '= ')
else if mas[i, j]=9 then
write( mas[i, j]:6, ‘=’, s[i, j])
else write(mas[i, j]:6, ‘=’, s[i, j);
end;
writeln;
end;
readkey;
end.
 

Write a program that calculates the length of a keyboard string.

Decision:

1
2
3
4
5
6
7
8
9
10
program characters_and_strings_6;
uses crt;
var
s: string;
i, n: integer;
begin
write('Enter a string (max. 50 sim.): ')read(s);
write('String ', s, ' contains ', length(s),' ' symbol(a)');
readkey;
end.

The user enters a string, if there are spaces at the beginning of this line, the program removes them.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
program characters_and_strings_7;
uses crt;
var
s: string;
i, n: integer;
begin
i:=0;
write('Enter the string > ')read(s);
while s[i+1]=‘ ‘ do i:=i+1;
delete(s, 1, i);
write(s);
readkey;
end.
 

Write a program that checks whether the string you enter is an integer.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
program characters_and_strings_8;
uses crt;
var
s: string;
i, n: integer;
begin
i:=1;
write('Enter the string > ')read(s);
while ((ord(s[i])>47)and(ord(s[i])<58))or(ord(s[i])=45 )or(ord(s[i])=43) do
i:=i+1;
if i1=length(s) then write('String is an integer')
else write('String is not an integer');
readkey;
end.

The program calculates the user-entered value of an expression of type A1 Z1 A2 Z2 A3 Z3 ... Am Zm, where Ai is an integer between 0 and 9, and Zj is the + or -.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
program characters_and_strings_9;
uses crt;
var
example: string; i, n, znak, otv: integer;
begin
clrscr; randomize;
Write('Enter an example > ')read(example);
i:=3; n:=length(example); otv:=ord(example[1]);
while i<=do
begin
znak:=ord(example[i1]);
if znak=43 then otv:=otv+(ord(example[i])48)
else if znak=45 then otv:=otv(ord(example[])48)
else exit;
i:=i+2;
end;
otv:=otv48;
write(example,‘=’,otv);
readkey;
end.

The program requests a number in the hexadecimal number system, and then translates it into the decimal system.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
program characters_and_strings_10;
uses crt;
var
hex_str: string;
i, x, n, _int: integer;
dec: real;
begin
clrscr; randomize;
write('Enter the number in the SHS >')read(hex_str);
dec:=0; x:=0; n:=length(hex_str);
for i:=downto 1 do
begin
_int:=ord(hex_str[i]);
if (_int>47) and (_int<58) then
dec:=dec+(_int48)*(exp(ln(16)*x))
else if (_int>64) and (_int<71) then
dec:=dec+(_int55)*(exp(ln(16)*x))
else
begin write(‘Error ‘); exit end;
x:=x+1;
end;
write(hex_str,' in decimal CC = ', dec);
readkey;
end.

Compose a program that converts the user-entered number of the binary system to an equal decimal number.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

program characters_and_strings_11;
uses crt;
var
bin_str: string;
i, x, n, _int: integer;
dec: real;
begin
clrscr; randomize;
write('Enter a number in the DC > '); read(bin_str);
dec:=0; x:=0; n:=length(bin_str);
for i:=n downto 1 do
begin
_int:=ord(bin_str[i]);
if (_int=48) then dec:=dec+0
else if (_int=49) then dec:=dec+(1*(exp(ln(2) *x)))
else begin write(»); exit end;
x:=x+1;
end;
write(bin_str,' in decimal CC = ', dec);
readkey;
end.

Create the string str2 from the even characters of the string st1 and the string str3 from the odd characters.

Decision:

Enter a string of words. Display the words in reverse order.

Decision:

Count the number of words in the string entered by the user.

Decision: