Answers 3
Answers 3
4. Write an algorithm and a program to compute the VAT payable on an item. The
program should ask the user to enter the price of an item and compute the VAT
payable on the item using a fixed rate of 16% of the price.
A. program discount;
const rate := .16
var vat, price: real;
begin
writeln(Enter the price) ;
readln(price);
vat := price* rate;
write(vat);
end.
Check your progress 5
1. A store uses the following policy to award discount. If the customer purchases
products costing more than $500 she is given a discount of 10%, otherwise she
is given a discount of 2%. Write a program to compute the discount a customer
will receive based on the purchase she makes.
A. program discount;
const rate1 := .10
const rate2 := .02
var discount, price: real;
begin
writeln(Enter the price) ;
readln(price);
if price>500 then
discount := price* rate1;
else
discount:= price *rate2;
write(vat);
end.
2. Write a program to print the message Your tuition is wavered if the student
scores over 85 marks in the entrance examination.
program tuition;
var score: integer;
begin
writeln(Enter score);
readln(score);
if score>85 then
writeln(Your tuition is wavered);
end.
3. Consider the following while loop:
Begin
a := 0;
while a < 5 DO
begin
a := a + 3;
Writeln(a, 'is less than 5');
end;
end.
How many times would the program go through the loop?
A. The program will go through the loop two times.
4. Consider the following segment of code:
Begin
for a := 1 to 5 do
Writeln('Help');
Writeln('me);
end.
Is the above statement correct? Give reason for your answer.
A. No. The two Writeln statements should be between a begin and end
statement. The correct code is:
begin
For a:= 1 to 5 do
begin
Writeln(Help);
Writeln(me);
end;
end.
5. Consider the following segment of code:
Begin
a := 0;
repeat
a := a + 1;
Writeln(The number is , a);
until a > 5;
<End computer font>
In the third repetition of the repeat-until segment, what is printed out?
A. It will print 3.
Check your progress 6
1. Construct a trace table for the following segment of code:
Begin
a :=1;
b := 2;
c := 1;
while a < 20 DO
begin
b := a * 2;
c := b + 1;
a : = c + 2;
end;
end.
A.
A
B
C
1
2
1
2
3
5
10
11
13
20
21
23
10
11
12
13
7. Using the flowchart you created for Q18 in Section 2s End-of-section Questions,
write a program in Pascal that will accept 25 integers and displays the number of
positive and negative numbers.
A. program positive_negative;
var num, square, cube: integer;
begin
for i := 1 to 25 do
begin
readln(num);
if num>0 then
poscount=poscount+1;
else
negcount=negcount+1;
end;
writeln(number of positive numbers is, poscount);
writeln(number of negative numbers is, negcount);
end.
14
8. a. Write a pseudocode that will accept a set of marks of students and display the
highest marks. The pseudocode stops when the user input -1 for marks.
b. Using the pseudocode you created in your answer Q8a as a guide, implement
the program in Pascal.
A. a. Step 1: start
Step 2: largest 0
Step 3: read num
Step 4: while num <> -1
If num>largest then
largest num
endif
read num
endwhile
Step 5: write largest
Step 6: stop
b. program highest;
var i, num, mark: integer;
begin
largest := 0;
readln(enter number);
read(num);
while num< >-1 do
begin
If num>largest then
largest =num;
readln(enter number);
read(num);
end;
end.
15
16
10.Write a program in Pascal that will read sales of 50 sales men into an array and
display the sales of all sales men who have total sales more than $10,000.
A. program sales;
var
i: integer;
scores: [1 to 50] of integer;
begin
write(Storing sales in an array)
for i: = 1 to 50 do
begin
write(Enter sales);
readln(sales[i]);
end;
write (Reading sales from the array and checking for sales>1000)
for i: = 1 to 50 do
begin
if sales[i] >1000 then
writeln(sales[i]);
end;
end.
17