F5 Pascal Programming Revision – 19 Feb 2002 ARITHMETIC OPERATOR +, - , *, / div : integer division mod : get the remainder (integer) sqrt, round, trunc, random, abs e.g trunc(53.2) div 10 = 5 trunc(53.2) mod 10 = 3 LAB 1 : Changing Coins Ask for user input (e.g. 52.3) $10 : 5 $5 : 0 $2 : 1 $1 : 0 50cents : 0 20cents : 1 10 cents : 1 ANSWER program coin(input,output); var num : real; dollar, cent : integer; begin write('Enter Money to change : '); readln(num); dollar := trunc(num); cent := trunc(num*10)-(dollar*10); writeln('Change'); writeln('$10 : ', dollar div 10); writeln('$5 : ', (dollar mod 10) div 5 ); writeln('$2 : ', ((dollar mod 10) mod 5) div 2); writeln('$1 : ', ((dollar mod 10) mod 5) mod 2 ); writeln('50c : ', cent div 5); writeln('20c : ', (cent mod 5) div 2); writeln('10c : ', (cent mod 5) mod 2); readln; end. LAB 2 : Calculator (using for loop) This is a calculator capable to perform +, -, x, /, div, mod, power ======================================= Enter the first number : 10.0 Enter the operation : d Enter the second number : 2.0 --------------------------------------The integer quotient of 10.0 divided by 2.0 is 5.0 ANSWER program calculator (input, output); var num1, num2 : real; oper : char; i, j : integer; power : real; begin P.1 of 5 F5 Pascal Programming Revision – 19 Feb 2002 writeln('This is a calculator capable to perform'); writeln('+, -, x, /, div, mod'); writeln('======================================='); write('Enter the first number : '); readln(num1); write('Enter the operation : '); readln(oper); write('Enter the second number : '); readln(num2); writeln('---------------------------------------'); if oper='+' then writeln('The sum of ', num1:1:1, ' and ', num2:1:1, ' is ', num1+num2:1:1); if oper='-' then writeln('The difference of ', num1:1:1, ' and ', num2:1:1, ' is ', (num1-num2):1:1); if oper='x' then writeln('The product of ', num1:1:1, ' and ', num2:1:1, ' is ', num1*num2:1:1); if oper='/' then writeln('The quotient of ', num1:1:1, ' divided by ', num2:1:1, ' is ', num1/num2:1:1); if oper='d' then writeln('The integer quotient of ', num1:1:1, ' divided by ', num2:1:1, ' is ', trunc(num1) div trunc(num2)); if oper='m' then writeln('The remainder of ', num1:1:1, ' divided by ', num2:1:1, ' is ', trunc(num1) mod trunc(num2)); if oper='p' then begin power := num1; j := trunc(num2); for i := 1 to j do power := power * num1; writeln('num1:1:1, ' to the power of ', num2:1:0, ' is ', power:1:1); end; readln; end. CONDITIONAL STATEMENT EXAMPLE 1 if (boolean expression) then statement; EXAMPLE 2 if (boolean expression) then begin statement X1; statement X2; statement X3; end else statement Y; EXAMPLE 3 if (boolean expression) then statement X else if (boolean expression Y) then statement Y else statement Z; P.2 of 5 F5 Pascal Programming Revision – 19 Feb 2002 PRECEDENCE OF OPERATORS * = NOT NOT Operator Not / div mod and + - or > < >= <= <> X < 3 (X < 3) Precedence Highest High Low Lowest << ERROR << CORRECT X < 3 or Y > 4 (X < 3) or (Y > 4) << ERROR << CORRECT LAB 3 : PASSING MARK Ask for user input 1) which form, 2) which subject, and 3) the mark he/she got Determine whether he/she passes the subject. SAMPLE 1 Which FORM is the student studying? 1 Which SUBJECT is it? Maths What is his/her score? 68 Sorry! The F.1 student has failed in the subj. Maths SAMPLE 2 Which FORM is the student studying? 7 Which SUBJECT is it? Maths What is his/her score? 55 Congratulation! The F.7 student has passed the subj. Maths Form Chinese English Maths 1-3 4-5 6-7 60 50 40 60 50 40 70 60 50 ANSWER program passmark(input, output); var form : integer; subj : string; score : integer; pass : boolean; begin repeat write ('Which FORM is the student studying? '); readln (form); write ('Which SUBJECT is it? '); readln (subj); write ('What is his/her score? '); readln (score); if (subj='Maths') then P.3 of 5 F5 Pascal Programming Revision – 19 Feb 2002 begin if (form >= 1) and (form <= 3) and (score >= 70) then pass := true else if ((form = 4) or (form = 5)) and (score >= 60) then pass := true else if ((form = 6) or (form = 7)) and (score >= 50) then pass := true else pass := false; end else if (subj = 'Chi') or (subj = 'Eng') then begin if (form >= 1) and (form <= 3) and (score >= 60) then pass := true else if ((form = 4) or (form = 5)) and (score >= 50) then pass := true else if (score >= 40) then pass := true else pass := false; end; if pass then writeln('Congratulation! The F.',form,' student has passed the subj. ', subj) else writeln('Sorry! The F.',form,' student has failed in the subj. ', subj); writeln; writeln; until (form = 8); readln end. REPEATITIVE CONTROL STATEMENT While Loop, Repeat Loop, For Loop j:=100; For count := j downto -100 do BEGIN count := count + 1; <- unacceptable statement because changing the counter within for loop x := x + count; y := y + 1; END; LAB 4 STEP STEP STEP STEP 1 2 3 4 : : : : Ask user to input a integer (must be between 1 to 20) if input 1 , then output “user input a 1’ determine whether the number is a prime no. or composite no. repeat the above until user input 99 Algorithm user input a number N P.4 of 5 F5 Pascal Programming Revision – 19 Feb 2002 If N cannot be divided by 2,3,4, … or (N-1) then N is prime Else N is composite ANSWER program primeno(input, output); var num, i : integer; prime : boolean; begin repeat repeat write('Please enter an integer (1-20) : '); readln(num); if not ((num >= 1) and (num <= 20)) then writeln('Value out of range, please re-enter!'); until ((num >= 1) and (num <= 20)) or (num=99); if (num = 1) then writeln('you input 1') else if (num <> 99) then begin prime := true; for i:= 2 to (num-1) do if (num mod i) > 0 then prime := false; if prime then writeln (num, ' is a prime number.') else writeln (num, ' is a composite number.'); end; until (num = 99); end. P.5 of 5