PASCAL PROGRAMMING ASSIGNMENT 2
PASCAL PROGRAMMING ASSIGNMENT 2
…...
………………………………………..............................................................................................................
...................................................................................................................................................................
a) Write and writeln both print text on the screen. The difference is that write just prints
while writeln prints and then shifts the cursor to the first place of the next row on the
screen. For example write(‘Good morning’); writeln(‘Good morning’); After
displaying the text Good morning in writeln instruction the cursor will move to the next
row but in the write instruction it will not.
b) Read and readln- the user gets information from the keyboard. Both instructions can read
values and characters from the keyboard and directly store them into specified variables.
For example :
Program Example;
var num1, num2: integer;
begin
writeln(‘Please enter two numbers separated by a space’);
read(num1);
read(num2);
writeln(‘num1=’,num1,’num2=’, num2);
end.
When run the program will display the message Please enter two numbers separated by a
space. The user is expected to type two numbers eg 100 50 After which the program
shows num1=100 num2=50 if we replace the read instruction with the readln instruction
the program will assign 100 to num1 then discard the rest of the text that was typed and
then wait for more input to assign num2.
c) Procedure and function can be viewed as small programs, they have a set of parameters,
values or variables. A procedure is a set of instructions which takes input and performs a
certain task, it does not return a value. Function on the other hand is a set of instructions
which takes some input and performs certain tasks and it returns a value meaning it
calculates an answer which is made available. Therefore a procedure just executes
commands while function returns a value.
d) Variable and data type – variables are containers that hold value, they can also be viewed
as locations where data is stored during programming. Variables are declared before the
keyword ‘begin’ just after the program head. When variables are declared the name of
the variable is stated. Eg Program Example;
Var num 1, num 2: integer;
Begin
Data types on the other hand are digital variables that are manipulated by the
programmer. They are used together with the variables when they are declared. A
variable cannot be declared without a data type. Data types can be categorized as scalar,
pointer and structured data types.
e) DIV and MOD- div is division in which the fractional part is discarded. For example a
div b returns the integer part of the result after dividing two integers. 19 div 5 will return
a result of 3, the remainder is discarded. Mod stands for modulus and returns the
remainder when one integer is divided by another. For example 19 mod 5 will return a
value of 4.
QUESTION 2
a) Program Convert(input,output);
uses crt;
var a, b, ZWL, US$, rate:real;
begin
clrscr;
writeln(‘Please enter amount to be converted in ZWL’);
readln(amount in ZWL);
writeln(‘Please enter amount in US$’);
readln(US$);
writeln(‘Please enter today’s rate’);
readln(today’s rate);
end;
procedure calculate_currency;
begin
currency:= ZWL/today’s rate;
end;
Procedure display _answer;
begin
writeln(‘ The converted currency is’,a ZWL is equivalent to bUS$’);
readln
end.
b) Program largesmall(input,output);
uses crt;
var a, b, c, d, e, largest, smallest: integer;
begin
clrscr;
writeln(‘Enter first number’);
readln(a);
writeln(‘Enter second number’);
readln(b);
writeln(‘Enter third number’);
readln( c);
writeln(‘Enter fourth number’);
readln(d);
writeln(‘Enter fifth number’);
readln(e);
large:=a;
If b>large then begin
large:=b;
end;
If c>large then begin
large:=c;
end;
If d>large then begin
large:=d;
end;
If e>large then begin
large:=e;
end;
writeln(‘ the largest is large’);
readln;
end;
small:=a;
If b<small then begin
small:=b;
end;
If c<small then begin
small:=c;
end;
If d<small then begin
small:=d;
end;
If e<small then begin
small:=e;
end;
writeln(‘ the smallest is small’);
readln
end.