Solutions of Programming Exercise (1-20)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Solution of Question 1.

PROGRAM Circle;
USES Crt;

CONST
pi = 3.14;

VAR
r, Circum, Area: real;

BEGIN
clrscr;

write('Enter the radius of the circle: ');


readln(r);

Circum := 2 * pi * r;
Area := pi * r * r;

writeln('The circumference is: ', Circum:0:2);


writeln('The area is: ', Area:0:2);

readkey
END.

Solution of Question 2.
PROGRAM PosNegZeroChecker;
USES Crt;

VAR
number: real;

BEGIN
clrscr;

write('Enter a number: ');


readln(number);

if number > 0 then


writeln('This is a positive number.')
else if number < 0 then
writeln('This is a negative number.')
else
writeln('This is zero.');

readkey
END.
Solution of Question 3.
PROGRAM GreaterNumChecker;
USES Crt;

VAR
num1, num2: real;

BEGIN
clrscr;

write('Enter number 1: ');


readln(num1);

write('Enter number 2: ');


readln(num2);

if num1 > num2 then


writeln(num1:0:2, ' is greater than ', num2:0:2, '.')
else if num2 > num1 then
writeln(num2:0:2, ' is greater than ', num1:0:2, '.')
else
writeln(num1:0:2, ' and ', num2:0:2, ' are equal.');

readkey
END.
Solutions of Question 4.
PROGRAM SeriesWriter;
USES Crt;
VAR i: integer;

BEGIN
clrscr;

{ --- Solution 1 --- }

for i := 1 to 100 do
write(i, ' ');

writeln; writeln;

{ --- Solution 2 --- }

i := 1;

while i <= 100 do


begin
write(i, ' ');
i := i + 1
end;

writeln; writeln;

{ --- Solution 3 ---}

i := 1;

repeat
write(i, ' ');
i := i + 1
until i > 100;

writeln; writeln;

{ --- Solution 4 --- }

i := 0;

while i < 100 do


begin
i := i + 1;
write(i, ' ')
end;

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;

{ --- Solution 1 --- }

sum := 0;

for i := 1 to 100 do
if i mod 5 = 0 then
sum := sum + i;

writeln('The sum is: ', sum);


writeln; writeln;

{ --- Solution 2 --- }

i := 5; sum := 0;

while i <= 100 do


begin
sum := sum + i;
i := i + 5
end;

writeln('The sum is: ', sum);


writeln; writeln;

{ --- Solution 3 --- }

i := 5; sum := 0;

repeat
sum := sum + i;
i := i + 5
until i > 100;

writeln('The sum is: ', sum);


writeln; writeln;
{ --- Solution 4 --- }

sum := 0;

for i := 1 to 20 do
sum := sum + 5 * i;

writeln('The sum is: ', sum);


writeln; writeln;

{ --- Solution 5 --- }

i := 0; sum := 0;

while i < 100 do


begin
i := i + 5;
sum := sum + i
end;

writeln('The sum is: ', sum);


writeln; writeln;

{ --- Solution 6 --- }

i := 0; sum := 0;

repeat
i := i + 5;
sum := sum + i
until i >= 100;

writeln('The sum is: ', sum);

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;

writeln('The maximum is: ', max:0:2);

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;

writeln(evencnt, ' of the input numbers are even.');

readkey
END.
Solutions of Question 8.
PROGRAM FlexibleOddCounter;
USES Crt;
VAR
i, howmany, no, oddcnt: integer;

BEGIN
clrscr;

{ --- Solution 1 --- }

write('How many numbers will you enter: ');


readln(howmany);

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;

{ --- Solution 2 --- }

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;

write('Please enter an integer: ');


readln(no);

{ --- Solution 1 --- }

prime:=true;
for i := 2 to no-1 do
if no mod i = 0 then
begin
prime:=false;
break
end;

if prime then writeln(no, ' is a prime number.')


else writeln(no, ' is not a prime number.');

{ --- Solution 2 --- }

prime:=true;
for i := 2 to round(sqrt(no)) + 1 do
if no mod i = 0 then
begin
prime:=false;
break
end;

if prime=true then writeln(no, ' is a prime number.')


else writeln(no, ' is not a prime number.');

readkey
END.
Solution of Question 10.
PROGRAM RealRootsofQuadraticEquation;
USES Crt;
VAR
a, b, c, d, x1, x2: real;

BEGIN
clrscr;

write('Please enter a, b, c for ax2 + bx + c = 0: ');


readln(a,b,c);

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;

{ --- Solution 1 --- }

write('Enter an integer: '); readln(no);


write('The digits of ', no, ' are: ');

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);

{ Solution 1 works this way:


32374 div 10000 = 3 <==
32374 - 10000 * 3 = 2374
2374 div 1000 = 2 <==
2374 - 2 * 1000 = 374
374 div 100 = 3 <==
374 - 3 * 100 = 74
74 div 10 = 7 <==
74 - 7 * 10 = 4 <== }

{ --- Solution 2 --- }

write('Enter an integer: '); readln(str);


write('The digits of ', str, ' are: ');
sum := 0;
for i:= 1 to length(str) do
begin
write(str[i], ' ');
sum := sum + ord(str[i])-48
end;
writeln;
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;

write('Enter a name: ');


readln(fname);

{ --- Solution 1 --- }

vcount := 0; ccount := 0;

for i:= 1 to length(fname) do


if (fname[i]= 'a') or (fname[i]= 'e') or (fname[i]= 'i') or
(fname[i]= 'o') or (fname[i]= 'u') or
(fname[i]= 'A') or (fname[i]= 'E') or (fname[i]= 'I') or
(fname[i]= 'O') or (fname[i]= 'U') then
vcount := vcount + 1
else
ccount := ccount + 1;
writeln;
writeln('The number of vowels is: ', vcount);
writeln('The number of consonants is: ', ccount);

{ --- Solution 2 --- }

vcount := 0; ccount := 0;
fname := upcase(fname);

for i:= 1 to length(fname) do


if (fname[i]= 'A') or (fname[i]= 'E') or (fname[i]= 'I') or
(fname[i]= 'O') or (fname[i]= 'U') then
vcount := vcount + 1
else
ccount := ccount + 1;
writeln;
writeln('The number of vowels is: ', vcount);
writeln('The number of consonants is: ', ccount);
{ --- Solution 3 --- }

vcount := 0; ccount := 0;

for i:= 1 to length(fname) do


if fname[i] in ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
then
vcount := vcount + 1
else
ccount := ccount + 1;
writeln;
writeln('The number of vowels is: ', vcount);
writeln('The number of consonants is: ', ccount);

{ --- Solution 4 --- }

vcount := 0; ccount := 0;
fname := lowercase(fname);

for i:= 1 to length(fname) do


if fname[i] in ['a', 'e', 'i', 'o', 'u'] then
vcount := vcount + 1
else
ccount := ccount + 1;
writeln;
writeln('The number of vowels is: ', vcount);
writeln('The number of consonants is: ', ccount);

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 length(password) < 10 then valid:=false


else
begin
lcount := 0; ucount := 0; dcount := 0; pcount := 0;
for i:= 1 to length(password) do
if (password[i] >= 'a') and (password[i] <= 'z') then
lcount := lcount + 1
else if (password[i] >= 'A') and (password[i] <= 'Z') then
ucount := ucount + 1
else if (password[i] >= '0') and (password[i] <= '9') then
dcount := dcount + 1
else if password[i] in [',', ';', '.', '?', '!'] then
pcount := pcount + 1;

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;

write('Enter a car registration plate number: ');


readln(regplateno);

{ --- Solution 1 --- }

valid := true;

if length(regplateno) <> 6 then valid:=false;

for i := 1 to 2 do
if not ((regplateno[i] >= 'A') and (regplateno[i] <= 'Z')) then
valid:=false;

if regplateno[3] <> ' ' then valid:=false;

for i := 4 to 6 do
if not ((regplateno[i] >= '0') and (regplateno[i] <= '9')) then
valid:=false;

if not valid then writeln('NOT a valid car reg plate number.')


else writeln('This is a valid car registration plate number.');

{ --- Solution 2 --- }

if length(regplateno) <> 6 then valid:=false


else
if not ((regplateno[1] >= 'A') and (regplateno[1] <= 'Z')) or
not ((regplateno[2] >= 'A') and (regplateno[2] <= 'Z')) then
valid:=false
else
if regplateno[3] <> ' ' then valid:=false
else
if not ((regplateno[4] >= '0') and (regplateno[4] <= '9')) or
not ((regplateno[5] >= '0') and (regplateno[5] <= '9')) or
not ((regplateno[6] >= '0') and (regplateno[6] <= '9')) then
valid:=false;

if valid then writeln('This is a valid car reg plate number.')


else writeln('NOT a valid car registration plate number.');

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);

copy := text; {keep original text in copy}

text := lowercase(text); {turn text into lower case}


len := length(text);

{ --- Solution 1 --- }

palindrome := true;
for i := 1 to len div 2 do
if text[i] <> text[len + 1 - i] then
begin
palindrome := false;
break
end;

if palindrome then writeln('"', copy, '" is a palindrome.')


else writeln('"', copy, '" is NOT a palindrome.');

{ --- Solution 2 --- }

for i := 1 to len do
reverse[i] := text[len + 1 - i];

reverse[0] := text[0]; {copy length info}

if text = reverse then writeln('"', copy, '" is a palindrome.')


else writeln('"', copy, '" is NOT a palindrome.');

{ --- Solution 3 --- }

reverse := ''; {empty string}

for i := len downto 1 do


reverse := reverse + text[i]; {string concatenation}

if text = reverse then writeln('"', copy, '" is a palindrome.')


else writeln('"', copy, '" is NOT a palindrome.');

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;

avg := sum / HM;


writeln('The average is ', avg:0:2);

writeln('These are the numbers below the average: ');

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);

writeln('Here are the matching entries: ');


found:=false;
for i := 1 to cnt do
if (fname[i]=srchword) or (lname[i]=srchword) or
(telno[i]=srchword) then
begin
found:=true;
writeln(i, ': ', fname[i], ' ', lname[i], ' ', telno[i])
end;
if not found then writeln('-- Not Found Any Matching Entries
In The Directory --');
readkey
end;
3: begin
if cnt>0 then
begin
writeln('Here are all the people in the directory: ');
for i := 1 to cnt do
writeln(i, ': ', fname[i], ' ', lname[i], ' ', telno[i])
end
else
write('There is no record in the directory yet.');
readkey
end;
4: begin
gotoxy(1, 25); textcolor(15); textbackground(4);
write('Goodbye...');
readkey
end;
end;
until choice = 4;

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');

if (choice='A') or (choice='a') then


begin
for i:= 1 to 9 do
begin
swap:=false;
for j:=1 to 10-i do
if numbers[j] > numbers[j+1] then
begin
temp := numbers[j];
numbers[j] := numbers[j+1];
numbers[j+1] := temp;
swap := true;
end;
if swap=false then break; {already sorted, so stop sorting}
end
end
else
begin
for i:= 1 to 9 do
begin
swap:=false;
for j:=1 to 10-i do
if numbers[j] < numbers[j+1] then
begin
temp := numbers[j];
numbers[j] := numbers[j+1];
numbers[j+1] := temp;
swap := true;
end;
if swap=false then break; {already sorted, so stop sorting}
end
end;
if (choice='A') or (choice='a') then
write('The numbers after being sorted in ascending order: ')
else
write('The numbers after being sorted in descending order: ');

for i:= 1 to 10 do
write(numbers[i], ' ');

readkey
END.

Solution of Question 19.


PROGRAM FactorialFinder;
USES Crt;
VAR i, n, f: integer;
BEGIN
clrscr;

repeat
write('Enter n (a non-negative integer): ');
readln(n);
until n>=0;

if (n=0) or (n=1) then


write(n, '! = 1')
else
begin
f:=1;
for i:= 1 to n do
f:=f*i;
write(n, '! = ', f)
end;

readkey
END.
Solution of Question 20.
PROGRAM CombinationCalculator;
USES Crt;
VAR n, r, nCr: integer;

Function F(n: integer): longint;


Var i: integer;
Begin
if (n=0) or (n=1) then
F := 1
else
begin
F := 1;
for i:= 1 to n do
F:=F*i;
end
End;

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;

nCr := F(n) DIV (F(r) * F(n-r));

writeln(n, 'C', r, ' = ', nCr);

readkey
END.

You might also like