Professional Documents
Culture Documents
Solutions of Programming Exercise (1-20)
Solutions of Programming Exercise (1-20)
Solutions of Programming Exercise (1-20)
PROGRAM Circle;
USES Crt;
CONST
pi = 3.14;
VAR
r, Circum, Area: real;
BEGIN
clrscr;
Circum := 2 * pi * r;
Area := pi * r * r;
readkey
END.
Solution of Question 2.
PROGRAM PosNegZeroChecker;
USES Crt;
VAR
number: real;
BEGIN
clrscr;
readkey
END.
Solution of Question 3.
PROGRAM GreaterNumChecker;
USES Crt;
VAR
num1, num2: real;
BEGIN
clrscr;
readkey
END.
Solutions of Question 4.
PROGRAM SeriesWriter;
USES Crt;
VAR i: integer;
BEGIN
clrscr;
for i := 1 to 100 do
write(i, ' ');
writeln; writeln;
i := 1;
writeln; writeln;
i := 1;
repeat
write(i, ' ');
i := i + 1
until i > 100;
writeln; writeln;
i := 0;
writeln; writeln;
{ --- Solution 5 ---}
i := 0;
repeat
i := i + 1;
write(i, ' ')
until i >= 100;
readkey
END.
Solutions of Question 5.
PROGRAM SumofSeries5to100by5;
USES Crt;
VAR
i, sum: integer;
BEGIN
clrscr;
sum := 0;
for i := 1 to 100 do
if i mod 5 = 0 then
sum := sum + i;
i := 5; sum := 0;
i := 5; sum := 0;
repeat
sum := sum + i;
i := i + 5
until i > 100;
sum := 0;
for i := 1 to 20 do
sum := sum + 5 * i;
i := 0; sum := 0;
i := 0; sum := 0;
repeat
i := i + 5;
sum := sum + i
until i >= 100;
readkey
END.
Solution of Question 6.
PROGRAM Maxof10Inputs;
USES Crt;
VAR
i: integer;
no, max: real;
BEGIN
clrscr;
max := -999999;
for i := 1 to 10 do
begin
write('Enter number ', i, ': ');
readln(no);
if no > max then
max := no
end;
readkey
END.
Solution of Question 7.
PROGRAM EvenCounter;
USES Crt;
VAR
i, no, evencnt: integer;
BEGIN
clrscr;
evencnt := 0;
for i := 1 to 10 do
begin
write('Enter number ', i, ': ');
readln(no);
if no mod 2 = 0 then
evencnt := evencnt + 1
end;
readkey
END.
Solutions of Question 8.
PROGRAM FlexibleOddCounter;
USES Crt;
VAR
i, howmany, no, oddcnt: integer;
BEGIN
clrscr;
oddcnt := 0;
for i := 1 to howmany do
begin
write('Enter number ', i, ': ');
readln(no);
if no mod 2 = 1 then
oddcnt := oddcnt + 1
end;
writeln(oddcnt, ' of the ', howmany, ' input numbers are odd.');
writeln;
i:=0; oddcnt := 0;
repeat
i:=i+1;
write('Enter number ', i, ' (or -1 to exit): ');
readln(no);
if no <> -1 then
if no mod 2 = 1 then
oddcnt := oddcnt + 1
until no = -1;
writeln(oddcnt, ' of the ', howmany, ' input numbers are odd.');
readkey
END.
Solutions of Question 9.
PROGRAM PrimeChecker;
USES Crt;
VAR
i, no: integer;
prime: boolean;
BEGIN
clrscr;
prime:=true;
for i := 2 to no-1 do
if no mod i = 0 then
begin
prime:=false;
break
end;
prime:=true;
for i := 2 to round(sqrt(no)) + 1 do
if no mod i = 0 then
begin
prime:=false;
break
end;
readkey
END.
Solution of Question 10.
PROGRAM RealRootsofQuadraticEquation;
USES Crt;
VAR
a, b, c, d, x1, x2: real;
BEGIN
clrscr;
d := b*b - 4*a*c;
if d < 0 then
writeln('There are no real roots.')
else if d = 0 then
begin
x1 := (-b-sqrt(d))/(2*a);
writeln('There is only one real root. It is: ', x1:0:2)
end
else {if d > 0 then}
begin
x1 := (-b-sqrt(d))/(2*a);
x2 := (-b+sqrt(d))/(2*a);
writeln('There are two real roots. They are: ', x1:0:2, ' and ',
x2:0:2);
end;
readkey
END.
Solutions of Question 11.
PROGRAM SumOfDigitsOfAnInteger;
USES Crt, Math;
VAR i, no, digit, sum: integer; str: string;
BEGIN
clrscr;
sum := 0;
for i := 4 downto 1 do
begin
digit := no div round(power(10,i));
sum := sum + digit;
if digit <> 0 then write(digit, ' ');
no := no - round(power(10,i)) * digit
end;
writeln(no); {the last digit}
sum := sum + no;
writeln('And the sum of the digits is: ', sum);
readkey
END.
Solutions of Question 12.
PROGRAM VowelVSConsonantCount;
USES Crt;
VAR i, vcount, ccount: integer;
fname: string;
BEGIN
clrscr;
vcount := 0; ccount := 0;
vcount := 0; ccount := 0;
fname := upcase(fname);
vcount := 0; ccount := 0;
vcount := 0; ccount := 0;
fname := lowercase(fname);
readkey
END.
Solution of Question 13.
PROGRAM PasswordValidator;
USES Crt;
VAR i, lcount, ucount, dcount, pcount: integer;
password: string;
valid: boolean;
BEGIN
clrscr;
repeat
valid := true;
textcolor(7);
write('Enter a password: '); readln(password);
if not ((lcount >= 1) and (ucount >= 1) and (dcount >= 1) and
(pcount >= 1)) then
valid:=false
end;
if not valid then
begin
textcolor(3);
writeln('Not a valid password.');
writeln('Enter at least 1 lower case letter, 1 upper case
Letter, 1 digit and 1 punctuation symbol.');
writeln('And also make sure that the number of total characters
is at least 10.'); writeln
end;
until valid; {until valid=true;}
textbackground(4); textcolor(15);
writeln; writeln(password, ' is a valid password.');
readkey
END.
Solutions of Question 14.
PROGRAM NCCarRegPlateNoChecker;
USES Crt;
VAR i: integer; regplateno: string; valid: boolean;
BEGIN
clrscr;
valid := true;
for i := 1 to 2 do
if not ((regplateno[i] >= 'A') and (regplateno[i] <= 'Z')) then
valid:=false;
for i := 4 to 6 do
if not ((regplateno[i] >= '0') and (regplateno[i] <= '9')) then
valid:=false;
readkey
END.
Solutions of Question 15.
PROGRAM PalindromeChecker;
USES Crt;
VAR i, len: integer; text, copy, reverse: string; palindrome: boolean;
BEGIN
clrscr;
write('Enter a text to check whether it is a palindrome or not: ');
readln(text);
palindrome := true;
for i := 1 to len div 2 do
if text[i] <> text[len + 1 - i] then
begin
palindrome := false;
break
end;
for i := 1 to len do
reverse[i] := text[len + 1 - i];
readkey
END.
Solution of Question 16.
PROGRAM DisplayNumbersBelowAverage;
USES Crt;
TYPE tamsayi = integer;
CONST HM = 10;
VAR i, sum: tamsayi;
nums: array[1..HM] of tamsayi;
avg : real;
BEGIN
clrscr;
sum := 0;
for i := 1 to HM do
begin
write('Enter number ', i, ': ');
readln(nums[i]);
sum := sum + nums[i];
end;
for i := 1 to HM do
if nums[i] < avg then writeln(nums[i]);
readkey
END.
Solution of Question 17.
PROGRAM TelephoneDirectory;
USES Crt;
CONST UB = 3;
VAR fname, lname, telno: array[1..UB] of string;
srchword: string;
cnt, choice, i: integer;
found: boolean;
Procedure DisplayMenu;
Begin
clrscr;
writeln('Cyprus Telephone Directory');
writeln('--------------------------'); writeln;
writeln('1. Enter a new person.');
writeln('2. Search a person.');
writeln('3. List everybody.'); {Extra option}
writeln('4. Exit the program.'); writeln;
End;
BEGIN
cnt :=0 ;
repeat
DisplayMenu;
repeat
write('Enter your choice: '); readln(choice);
if not ((choice>=1) and (choice<=4)) then
writeln('Invalid choice. Pls enter 1, 2, 3, or 4.'); writeln
until (choice>=1) and (choice<=4);
case choice of
1: begin
if cnt < UB then
begin
cnt := cnt + 1;
write('Enter the first name: ');
readln(fname[cnt]);
write('Enter the last name: ');
readln(lname[cnt]);
write('Enter the phone number: ');
readln(telno[cnt])
end
else
begin
write('The directory is full, therefore it is not possible
to enter a new record.'); readkey
end
end;
2: begin
write('Enter a search word (firstname, surname or telno): ');
readln(srchword);
END.
Solution of Question 18.
PROGRAM SortASCorDESC;
USES Crt;
VAR numbers: array[1..10] of integer = (3,5,2,7,9,8,1,6,10,4);
i, j, temp: integer; choice: char; swap: boolean;
BEGIN
clrscr;
write('The numbers before being sorted: ');
for i:= 1 to 10 do
write(numbers[i], ' ');
writeln;
repeat
write('Do you want to search in Asc or Desc order (A/D)?: ');
readln(choice);
until (choice='A') or (choice='a') or (choice='D') or (choice='d');
for i:= 1 to 10 do
write(numbers[i], ' ');
readkey
END.
repeat
write('Enter n (a non-negative integer): ');
readln(n);
until n>=0;
readkey
END.
Solution of Question 20.
PROGRAM CombinationCalculator;
USES Crt;
VAR n, r, nCr: integer;
BEGIN
clrscr;
repeat
write('Enter n (a non-negative integer): ');
readln(n);
until n>=0;
repeat
write('Enter r (a non-negative integer): ');
readln(r);
until r>=0;
readkey
END.