Last Updated:

Pascal Programming Language - Example Codes [2]

Pascal Arrays - Example Codes

 

The program calculates the arithmetic mean of the even numbers of the array, the number of elements of which is set by the user.

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 arrays_1;
uses crt;
var
n, i: integer;
k, sum: real;
mas: array[1.. 100] of integer;
begin
clrscr;
randomize;
k:=0; sum:=0;
write('Number of elements > ')read(n);
for i:=1 to n do
begin
mas[i]:=random(10);
write(mas[i]:4);
if mas[i] mod 2=0 then
begin
k:=k+1;
sum:=sum+mas[i]
end;
end;
writeln;
write('Arithmetic mean: ', sum/k:4:2);
readkey;
end.

An array consisting of N keyboard-entered numbers is given. Find out if the sum of its elements is a multiple of six.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
program arrays_2;
uses crt;
var
n, i, sum: integer;
mas: array[1.. 100] of integer;
begin
clrscr;
sum:=0;
write('Number of elements >')read(n);
for i:=1 to n do
begin
write(i, ' element > ')read(mas[i]);
sum:=sum+mas[i];
end;
if sum mod 6=0 then
write('Sum of elements is a multiple of 6')
else write('Sum of elements is not a multiple of 6');
readkey;
end.

Generate and output an array of mas consisting of N elements. The value of mas[step] must be equal to the sum of the square of the step and its root square.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
program arrays_3;
uses crt;
var
n, i, sum: integer;
mas: array[1.. 100] of real;
begin
clrscr;
write('Number of elements > ')read(n);
for i:=1 to n do mas[i]:=sqr(i)+sqrt(i);
for i:=1 to n do
write(mas[i]:3:2, ‘ ‘);
readkey;
end.

Two arrays A and B are populated with random numbers. Form an array C consisting of the sum of the elements of the first two arrays (for example, C[1]=A[1]+B[1]...).

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 arrays_4;
uses crt;
var
n, i: integer;
a, b, c: array[1.. 100] of integer;
begin
clrscr;
randomize;
write('Array A: ');
for i:=1 to 5 do
begin
a[i]:=random(10);
write(a[i]:4);
endwriteln;
write('Array B: ');
for i:=1 to 5 do
begin
b[i]:=random(10);
write(b[i]:4);
c[i]:=a[i]+b[i];
endwriteln;
write('Array C: ');
for i:=1 to 5 do write(c[i]:4);
readkey;
end.

Generate and display a sequence of n elements specified by the random number sensor at interval [-23, 34].

Decision:

Find the maximum element of a one-dimensional array of random integers from -10 to 10. Display all values equal to the maximum and their indexes.

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 arrays_6;
uses crt;
const n=20;
var i, key, max: integer;
mas: array[1.. n] of integer;
begin
clrscr; randomize;
writeln('List:');
for i:=1 to n do
begin
mas[i]:=random(20)10;
write(‘ ‘,mas[i]);
end;
key:=1;
for i:=2 to n do
begin
if mas[i]>mas[key] then
key:=i;
max:=mas[key];
end;
writeln;
writeln('Index Element');
for i:=1 to n do
begin
if mas[i]=max then
writeln(mas[i]:4, i:12);
end;
readkey;
end.

The program checks for the presence of the element searched by the user in the array of random numbers.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
program arrays_7;
uses crt;
var
n, i, x: integer;
b: boolean;
A: array[1.. 100] of integer;
begin
clrscr;
randomize;
write('Number of elements > ')read(n);
write('Searchable element > ')read(x);
b:=false;
for i:=1 to n do
begin
A[i]:=random(15)write(A[i],‘ ‘);
if A[i]=then b:=true;
end;
writeln;
if b then write('Element with value ', x, 'is')
else write('Element with value ', x, ' not available');
readkey;
end.

Write a program that checks whether the entered sequence is increasing.

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 arrays_8;
uses crt;
var
n, i, j: integer;
b, c: boolean;
A: array[1.. 100] of integer;
begin
clrscr;
randomize;
write('Number of elements > ')read(n);
for i:=1 to n do
begin
write(i, ' element > ')read(A[i]);
end;
b:=true; c:=false;
for i:=1 to n1 do
begin
if A[i+1]<A[i] then b:=false;
for j:=2 to n do if A[i]<>A[j] then c:=true;
end;
if b and c then write('This is an increasing sequence')
else write('It is not an increasing sequence');
readkey;
end.

Check whether there are elements with the same values in the array filled with random numbers.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
program arrays_9;
uses crt;
var
n, i, j: integer;
b: boolean;
A: array[1.. 100] of integer;
begin
clrscr;
randomize;
b:=false;
write('Number of elements > ')read(n);
for i:=1 to n do
begin
A[i]:=random(10)write(A[i], ‘ ‘);
if i>1 then for j:=i1 downto 1 do
if A[i]=A[j] then b:=true;
end;
if b then write('There are identical elements in the array')
else write('There are no identical elements in the array');
readkey;
end.

Write a program that sorts the array in descending order using the Bubble method.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
program arrays_10;
uses crt;
var mas:array[1.. 100] of integer;
n, i, m, k, x: integer;
begin
clrscr;
write('Number of elements > ');
read(N);
for i:=1 to n do
begin
write(i, ' element > ')read(mas[i]);
end;
for k:=downto 2 do
begin
m:=1{ m — place max }
for i:=2 to k do if mas[i]>mas[m] then m:=i;
x:=mas[m]; mas[m]:=mas[k]; mas[k]:=x;
end;
writeln('Sorted Array:');
for i:=1 to n do write(mas[i],‘ ‘);
readkey;
end.

Write a program that sorts the array ascending by the selection method.

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 arrays_11;
uses crt;
var i, j, nom, chislo, n: Integer;
mas: array[1.. 100] of Integer;
begin
write('Number of elements > ')read(n);
for i:=1 to n do
begin
write(i,' element > ')read(mas[i]);
end;
for i:=1 to n do
begin
chislo:=mas[i]; nom:=i;
for j:=i+1 to n do
begin
if (mas[nom]>mas[j]) then nom:=j;
end;
if (nom<>i) then
begin
mas[i]:=mas[nom];
mas[nom]:=chislo;
end;
end;
writeln('Sorted Array:');
for i:=1 to n do write(mas[i], ‘ ‘);
readkey;
end.

Write a program that sorts the array in ascending order by insertion.

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 arrays_12;
uses crt;
var mas: array[1.. 100] of integer;
i, j, _var, nom, n: integer;
begin
write('Number of elements > ')read(n);
for i:=1 to n do
begin
write(i,' element > ')read(mas[i]);
end;
for i:=1 to n1 do
begin
nom:=i+1;
_var:=mas[nom];
for j:=i+1 downto 2 do
begin
if (_var<mas[j1]) then
begin
mas[j]:=mas[j1];
nom:=j1;
end;
end;
mas[nom]:=_var;
end;
writeln('Sorted Array:');
for i:=1 to n do
write(mas[i], ‘ ‘);
readkey;
end.

Find the product of the elements of a one-dimensional array consisting of n elements. Items are entered from the keyboard.

Decision:

Make a program that uses binary search to check for the presence of the element you are looking for in the array.

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 arrays_14;
uses crt;
var i, n, key, a, b, c: integer;
x: boolean; mas: array[1.. 100] of integer;
begin
write('Array size > ')read(n);
write('Searchable element > ');
read(key);
for i:=1 to n do
begin
mas[i]:=i*n;
write(mas[i],‘ ‘);
end;
a:=1; b:=n; x:=false;
while a<=do
begin
c:=a+(ba) div 2;
if (key<mas[c]) then
b:=c1
else if (key>mas[c]) then
a:=c+1
else
begin x :=truebreakend;
end;
if (x=true) then writeln('Element found')
else write('Element not found');
end.

Calculate the sum of the elements of each line entered from the keyboard of a two-dimensional array.

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 arrays_15;
uses crt;
var i, j, r, c, counter: integer;
mas: array[1.. 100, 1.. 100] of integer;
rmas: array[1.. 100] of integer;
begin
write('Number of lines > ')read(r);
write('Number of columns > ')read(c);
counter:=1;
for i:=1 to r do
begin
for j:=1 to c do
begin
write('Element No', i, j,' > ')read(mas[i, j]);
rmas[counter]:=rmas[counter]+mas[i, j];
end;
counter:=counter+1;
end;
writeln('Array:');
for i:=1 to r do
begin
for j:=1 to c do
write(mas[i, j],‘ ‘);
writeln;
end;
for i:=1 to r do
writeln('Sum of elements ', i, ' lines: ', rmas[i]);
readkey;
end.

The user enters a square matrix. The program determines the sum of the elements of its main diagonal.

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 arrays_16;
uses crt;
var i, j, n, d: integer;
A: array[1.. 100, 1.. 100] of integer;
begin
write('Number of rows and columns> ')read(n);
d:=0;
for i:=1 to n do
begin
for j:=1 to n do
begin
write('Element No', i, j,' > ')read(A[i, j]);
end;
end;
writeln('Square Matrix:');
for i:=1 to n do
begin
for j:=1 to n do
begin
write(A[i, j],‘ ‘);
if i=then d:=d+A[i, j];
end;
writeln;
end;
write('Sum of principal elements: ', d);
readkey;
end.

A matrix is entered from the keyboard, the size and elements of which are determined by the user. You need to transpose 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
program arrays_17;
uses crt;
var i, j, r, c: integer;
A: array[1.. 100, 1.. 100] of integer;
begin
write('Number of lines > ')read(r);
write('Number of columns > ')read(c);
for i:=1 to r do
begin
for j:=1 to c do
begin
write('Element No', i, j,' > ')read(A[i, j]);
end;
end;
writeln('Source Matrix:');
for i:=1 to r do
begin
for j:=1 to c do
write(A[i, j],‘ ‘);
writeln;
end;
writeln('Transposed matrix:');
for i:=1 to c do
begin
for j:=1 to r do
write(A[j, i],‘ ‘);
writeln;
end;
readkey;
end.

Find the largest and smallest elements in a two-dimensional array and replace all the smallest ones with the largest ones and vice versa.

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
program arrays_18;
uses crt;
var i, j, r, c, least, most: integer;
A: array[1.. 100, 1.. 100] of integer;
begin
write('Number of lines > ')read(r);
write('Number of columns > ')read(c);
for i:=1 to r do
begin
for j:=1 to c do
begin
write('Element No', i, j,' > ')read(A[i, j]);
end;
end;
least:=A[1, 1]; most:=A[1, 1];
writeln('Source Matrix:');
for i:=1 to r do
begin
for j:=1 to c do
begin
write(A[i, j],‘ ‘);
if A[i, j]>most then most:=A[i, j];
if A[i, j]<least then least:=A[i, j];
endwriteln;
end;
writeln('Largest element', most);
writeln('Smallest element', least);
writeln('Result:');
for i:=1 to r do
begin
for j:=1 to c do
begin
if least=A[i, j] then A[i, j] :=most
else if most=A[i, j] then A[i, j]:=least;
write(A[i, j],‘ ‘);
endwriteln;
end;
readkey;
end.

Arrange the elements in odd rows of the matrix in ascending order, and in even rows in descending order.

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 arrays_19;
uses crt;
var i, j, k, p, ch, key1, key2: integer;
matr: array[1.. 10,1.. 10] of integer;
begin
clrscr; randomize;
writeln('Source Matrix:');
for i:=1 to 10 do begin
for j:=1 to 10 do begin
matr[i, j]:=random(10){filling the matrix with pseudorandom numbers}
write(matr[i, j]:3);
endwriteln;
end;
for i:=1 to 10 do begin
for j:=1 to 10 do begin
for k:=1 to 9 do begin
key2:=k;
ch:=matr[j, k];
if j mod 2 <> 0 then {if not even, then execute... }
begin
if matr[j, key2]>matr[j, k+1] then key2:=k+1;
matr[j, k]:=matr[j, key2];
matr[j, key2]:=ch
end else { if even, then execute...}
begin
if matr[j, key2]<matr[j, k+1] then key2:=k+1;
matr[j, k]:=matr[j, key2];
matr[j, key2]:=ch;
endendendend;
writelnwrite('Sorted matrix:');
writeln;
for i:=1 to 10 do begin
for j:=1 to 10 do begin
write(matr[i, j]:3);
endwritelnend;
end.

Pascal  Branching operators - Example Codes

 

The program checks for parity the integer entered from the keyboard.

Decision:

1
2
3
4
5
6
7
8
9
10
program choice_1;
uses crt;
var a: integer;
begin
clrscr;
write('Enter the number of >')readln(a);
if a mod 2 <> 0 then write(a, ' odd number')
else write(a, ' is an even number');
readkey;
end.

 

Write a program that asks the user for age, and gives information about whether he is an adult.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
program choice_2;
uses crt;
var year: integer;
begin
clrscr;
write('Your age >')readln(year);
if (year > 0) and (year < 18) then
write('You are under the age of majority')
else if (year > 17) and (year < 150) then
write('You have reached the age of majority')
else write('Probably incorrect data');
readkey;
end.

The program calculates the quotient of dividing two numbers. But before doing this, she must check the correctness of the entered data.

Decision:

1
2
3
4
5
6
7
8
9
10
11
program choice_3;
uses crt;
var a, b: real;
begin
clrscr;
write('Numerator >')readln(a);
write('Denominator > ')readln(b);
if b = 0 then write('Cannot be divided by zero')
else write(a, '/', b, '=', a/b:3:1);
readkey;
end.

The user answers the question of the program: "How old are you" (0<x<112), after which it gives the message "You are x years old". It should be noted that different declensions are used for different numbers, for example, "32 years", "99 years", etc.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
program choice_4;
uses crt;
var year: integer;
begin
clrscr;
write('How old are you? > ')readln(year);
if (year>111) or (year<1) then write('Limit exceeded (111 years)')
else if (year mod 10=1) and (year<>11and (year<>111) then
write('To you ', year, 'year')
else if (year<5) xor ((year>21) and ((year mod 10=2) or (year mod 10=3) or (year mod 10=4)))
then write('You ', year, 'years' )
else write('To you ', year, 'years');
readkey;
end.

The buyer receives a 5% discount on the purchase if its value exceeds 500 USD, and a 10% discount if 1000 USD.

Decision:

1
2
3
4
5
6
7
8
9
10
11
program choice_5;
uses crt;
var p: real;
begin
clrscr;
write('Purchase price >')readln(p);
if (p>500) and (p<=1000) then write('Discount 5%: ', p(p/100*5))
else if p>1000 then write('Discount 10%: ', p(p/100*10))
else write('Discount no: ', p);
readkey;
end.

Write a program that displays the name of the month, after entering its number. For example, 3 – March, 10 – October.

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 choise_6;
uses crt;
var m:integer;
begin
clrscr;
write('Enter the month number > ')read(m);
case m of
1: write('January');
2: write('February');
3: write('March');
4: write('April');
5: write('May');
6: write('June');
7: write('July');
8: write('August');
9: write('September');
10: write('October');
11: write('November');
12: write('December')
else write('Error')
end;
readkey;
end.

The program, depending on the user's choice, performs one of four arithmetic operations on two numbers: addition, subtraction, multiplication or division.

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
program choise_7;
uses crt;
var a, b: real;
c: integer;
begin
clrscr;
write(‘a > ‘)read(a);
write(‘b > ‘)read(b);
writeln('1 is addition');
writeln('2 is subtraction');
writeln('3 is multiplication');
writeln('4 is division');
write('Operation number > ')read(c);
case c of
1: write(a:3:1,‘+’,b:3:1,‘=’, a+b:3:1);
2: write(a:3:1,‘-‘,b:3:1,‘=’, ab:3:1);
3: write(a:3:1,‘*’,b:3:1,‘=’, a*b:3:1);
4: write(a:3:1,‘/’,b:3:1,‘=’, a/b:3:1);
else write(‘Error’)
end;
readkey;
end.

A four-digit number is given. Check if it is a palindrome. Note: A palindrome is a number, word, or text that is read equally from left to right and right to left. For example, in our case, these are the numbers 1441, 5555, 7117, and so on.

Decision:

A number of type byte is given. Check whether the palindrome is its binary representation, taking into account the fact that the older zeros are preserved. An example of such numbers is: 102 (since 102 = 0110 01102, and this is a palindrome), 129 (129 = 1000 00012), etc.

Decision:

Two numbers are given. Determine the relationship between them (greater, less, equal).

Decision:

1
2
3
4
5
6
7
8
9
10
11
12
program choise_10;
uses crt;
var A, B: integer;
begin
clrscr;
write(‘A > ‘)read(A);
write(‘B > ‘)read(B);
if A>B then write(A, '>', B)
else if A<B then write(A, '<', B)
else write(A, '=', B);
readkey;
end.

Display the name of the day of the week by its number.

Decision:

The user enters a number corresponding to the day of the week, the program reports whether it is a working day or a day off.

Decision:

1
2
3
4
5
6
7
8
9
10
11
program choise_12;
uses crt;
var day: integer;
begin
clrscr;
write(' Day of the Week (1-7) > ')read(day);
if (day>0) and (day<6) then write(' Work day')
else if (day=6) or (day=7) then write ('Day off')
else write('Error');
readkey;
end.

The real numbers a, b and c are given, and a is different from 0. Solve the quadratic equation ax2 + bx + c = 0 or report that there are no valid solutions.

 

Decision: