Download (a) IF-THEN

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Elementary mathematics wikipedia , lookup

Transcript
CH 4 – SELECTION CONTROL STRUCTURE
IF <Condition> THEN
<statement A>
ELSE
<statement B> ;
false
Condition?
statement B
true
statement A
Selection between 2 actions
Example 4.1
Program PassOrFail;
var
mark : integer;
begin
START
write(‘Enter Mark: ‘);
readln(Mark);
‘Enter Mark’
Input mark
Mark>=
>=50
50then
then
ififMark
writeln(‘Pass’)
writeln(‘Pass’)
else
else
writeln(‘Fail’)
writeln(‘Fail’)
end.
false
Mark >= 50?
true
Output “Pass”
Output “Fail”
END
IF <Condition> THEN
BEGIN
<statement A1>;
<statement A2>;
<statement A3>
END
ELSE
BEGIN
<statement B1>;
<statement B2>;
<statement B3>
END;
compound statements
More than one statement is
required to be executed when the
condition is/is not satisfied.
false
compound statements
compound statements
Condition?
true
statement B1
statement A1
statement B2
statement A2
statement B3
statement A3
compound statements
IF <Condition> THEN
BEGIN
<statement A1>;
<statement A2>;
<statement A3>
END
ELSE
BEGIN
<statement B1>;
<statement B2>;
<statement B3>
END;
Program Convert;
var
choice: integer;
height : real;
begin
write(‘Enter a choice: (1, for inch to cm, 2 for cm to inch) ‘);
readln(choice);
START
‘Enter your choice’
Input choice
false
true
choice =1?
choice =
=1
1 then
then
ifif choice
begin
begin
Enter height
Enter height
writeln(‘Enter
your
height
in
inch:
’);
writeln(‘Enter your height in inch: ’);
cm
inch
readln(Height);
readln(Height);
writeln(Height:0:2,‘ inch=
‘ inch=‘,‘,Height
Height* *2.54:0:2,
2.54:0:2,‘ cm’)
‘ cm’)
writeln(Height:0:2,
Input height
Input height
end end
incm
in inch
else else
beginbegin
writeln(‘Enteryour
yourheight
heightinincm:
cm:’);’);
output height
output height
writeln(‘Enter
readln(Height);
inch
cm
readln(Height);
writeln(Height:0:2,‘ cm=
‘ cm=‘, ‘,Height/2.54:0:2,
Height/2.54:0:2,‘ inch’)
‘ inch’)
writeln(Height:0:2,
end
end
end.
END
To do or not to do an action
true
Condition?
statement A
false
IF <condition> THEN
<statement A>;
START
display “Enter conduct”
Input conduct
Conduct>90?
false
program CheckMerit;
Var
conduct : integer;
Begin
write(‘Enter conduct: ‘);
readln(conduct);
conduct> >9090then
then
ififconduct
writeln(‘You
have
a MERIT’);
writeln(‘You
have
a MERIT’);
writeln(‘Bye’)
end.
Bye
END
true
output ‘merit’
multiple selection
Condition?
false
IF <condition> THEN
<statement A>;
true
statement A
0-49
Poor
50-69
Satisfactory
70-89
Good
90-100 Excellent
START
display “Enter conduct”
program MultipleSelection;
var
conduct : integer;
Begin
write(‘Enter conduct: ‘);
readln(conduct);
if (conduct >=0) and (conduct < 50) then
writeln(‘Poor’);
writeln(‘Poor’);
if (conduct >=50) and (conduct < 70) then
writeln(‘Satisfactory’);
writeln(‘Satisfactory’);
if (conduct >=70) and (conduct < 90) then
writeln(‘Good’);
writeln(‘Good’);
if (conduct >= 90) then
writeln(‘Excellent’)
writeln(‘Excellent’)
end.
input conduct
conduct>=0
and <50?
false
conduct>=50
and <70?
false
conduct>=70
and <90?
false
conduct=90
false
END
true
output ‘poor’
true
output ‘satisfactory’
true
output ‘Good’
true
output ‘Excellent’
compound statements
To do or not to do a series of actions
IF <condition> THEN
BEGIN
<statement A1>;
<statement A2>;
<statement A3>
END;
Program division;
var
x, d, q : integer;
begin
writeln(‘enter number and divisor’);
readln(x, d);
ififdd<>
<>00then
then
begin begin
q := x/d;
q := x/d;
writeln(‘The
quotient
is ‘, q:10:2)
writeln(‘The
result
is ‘, q:10:2)
end end
end.
true
Condition?
statement A1
false
statement A2
statement A3
START
display “Enter number and divisor”
input number and divisor
D <> 0?
true
Divide number by divisor
false
Display result
END
GAMBLING- BIG AND SMALL
(a) A user guesses Big or Small or All equal
(b) Program generates 3 numbers randomly (representing results of 3 dice throws).
record the result as small in the variable result if the sum of the 3 numbers is less than 10.
record the result as big in the variable result if the sum of the 3 numbers is greater than 10
record the result as equal in the variable result if the 3 numbers are the same.
(c)Compare your guess with the result. If they match, you will win. Otherwise you lose.
Program gamble;
var dice1, dice2, dice3: integer;
result, ans : char;
begin
writeln(‘guess B(ig) or S(mall) or A(ll) ?’);
readln(ans);
randomize;
dice1 := random(6)+1;
dice2 := random(6)+1 ;
dice3 := random(6)+1;
if (dice1+dice2+dice3) < 10 THEN
result := ‘S’;
if (dice1+dice2+dice3) > 10 THEN
result := ‘B’;
if (dice1=dice2) AND (dice2 =dice3) THEN
result := ‘A’;
if ans = result then
writeln(‘you win’)
else
writeln(‘you lose’)
end.
Example : guessing if a number is even or odd.
program GuessEvenOdd;
var
num : integer;
ans , result: char;
begin
randomize;
num := random(48)+1;
if num mod 2 = 0 then
result := ‘E’
else
result := ‘O’;
writeln(‘Guess even or odd? E or O’);
readln(ans);
if ans = result then
writeln(‘win’)
else
writeln(‘loss’)
end.
Example : computer draws a character from A to E. Users guesses. If the guess is
correct, display correct message. Otherwise, display incorrect message.
program drawcharacter;
var
ch, ans : char;
begin
writeln('guess a character from A to E ....');
readln(ans);
randomize;
writeln('drawing a character from A to E ...');
ch :=chr( 65+random(5));
writeln('the character drawn is ', ch)
if ch = ans then
writeln('correct')
else
writeln('incorrect');
MILLIONAIRE
The program assign you $0 initially.
Then it generates 2 numbers from 1 to 3.
Then you guess the two numbers.
If each guess is correct, you gain $5000 and continue next guess. Otherwise, you must leave!
Program MILLIONAIRE;
var num1, num 2, guess, money: integer;
begin
randomize;
num1 := random(3)+1;
num2 := random(3)+1 ;
money:=0;
writeln(‘Guess the first number);
readln(guess);
if guess = num1 then
begin
money := money+1000;
writeln(‘Guess the second number);
readln(guess);
if guess = num2 then
money := money+2000
else
writeln(‘You must leave’)
end
else
writeln(‘You must leave’)
end.
Homework
Q1. Write a program that
asks you to enter length in kilometer.
asks you to enter a choice of converting the length into either meter or centimeter.
Display the length in the specified unit of measurement.
The output screen is as follows:
First run
Second run
Enter length in km
4
Enter a choice: 1. km -> m
2
answer in cm is 400000
2. km->cm
Enter length in km
4
Enter a choice: 1. km -> m
1
answer in m is 4000
2. km->cm
program convertkm;
var
km, m, cm : real;
choice : integer;
begin
writeln(‘enter length in km’);
readln(km);
writeln(‘enter a choice 1. km -> m
2. km’ );
readln(choice);
if choice = 1 then
begin
m := km * 1000;
writeln(‘answer in m is ’ , m:0:2)
end
else
begin
cm := km * 100000;
writeln(‘answer in cm is ’ , cm:0:2)
end
end.
Q2. Write a program that
(a) Display a menu of a coke and a sprit and asks you to make a choice.
(b) Asks you to enter amount of money.
(c) Display the change according to the choice of drinks made, if any.
(d) If not enough money is inserted, a message is displayed.
The output screen is as follows:
First run:
1. COKE $5 2. SPRITE $6
1
Enter your money:
7
the change is $ 2
Second run:
1. COKE $5 2. SPRITE $6
1
Enter your money:
4
Not enough money!
Third run:
1. COKE $5 2. SPRITE $6
1
Enter your money:
5
program vendermachine;
varchoice, price, money : integer;
begin
writeln(‘1. coke $5 2. sprite $6’);
readln(choice);
if choice = 1 then
price := 5
else
price := 6;
writeln(‘enter your money:’ );
readln(money);
if money > price then
writeln(‘the change is $’ , money-price);
if money < price then
writeln(‘not enough money!’ );
end.
Q3 Write a program that
(a) Display the amount in your saving bank account.
(b) Asks you to enter amount of money to withdraw.
(c)Display the message if you withdraw money with an amount greater than your
savings.Otherwise, display your amount in your saving after withdrawal.
The output screen is as follows:
First run:
you have $10000 savings in your bank
enter hk $ for withdraw:
12000
you have not enough saving!!
Second run:
you have $10000 savings in your bank
enter hk $ for withdraw:
8000
now in your account $2000
Third run:
you have $10000 savings in your bank
enter hk $ for withdraw:
10000
you can not take all money out! Must have money
program withdrawal;
var balance, money : integer;
begin
balance := 10000;
writeln(‘you have $’, balance, ‘ savings in your bank’);
writeln(‘enter hk $ for withdraw:’);
readln(money);
if money > balance then
writeln(‘you have not enough saving!!’);
if money = balance then
writeln(‘you can not take all money out! must have money’ );
if money < balance then
writeln(‘now in your account $’, balance-money );
end.
program withdrawal;
var balance, money : integer;
begin
balance := 10000;
writeln(‘you have $’, balance, ‘ savings in your bank’);
writeln(‘enter hk $ for withdraw:’);
readln(money);
if money > balance then
writeln(‘you have not enough saving!!’)
else
if money = balance then
writeln(‘you can not take all money out! must have money’ )
else
writeln(‘now in your account $’, balance-money );
end.
Q4
Given that
$1 HK = $0.12837 US
$1 HK = $1.05 RMB(人民幣)
Write a program that
Asks you to enter your amount in HK currency.
Asks you to make a choose from converting HK dollar to either US dollars or RMB.
Display your amount in the new currency.
The output screen is as follows:
First run:
Second run:
How many HK dollars you want to change?
400
Which currency you want? 1. US 2. RMB
1
You have US $51.35
How much HK dollars you want to change?
400
Which currency you want? 1. US 2. RMB
2
You have RMB $420.00
program currencychange;
var
hkdollar : real;
choice : integer;
begin
writeln(‘How many HK dollars you want to change?’);
readln(hkdollar);
writeln(‘Which currency you want? 1. US 2. RMB’ );
readln(choice);
if choice = 1 then
writeln(‘You have US $’ , hkdollar* 0.12837 :0:2)
else
writeln(‘You have RMB $’ , hkdollar*1.05:0:2)
end.
NESTED IF STATEMENTS
IF <Condition C1> THEN
IF <Condition C2> THEN
<statement A1>
ELSE
<statement A2>
ELSE
IF <Condition C3> THEN
<statement B1>
ELSE
<statement B2>;
false
false
Condition C3?
statement B2
true
Condition C1?
true
false
Condition C2?
statement B1 statement A2
0-49
Poor
50-69
Satisfactory
70-89
Good
90-100
Excellent
true
statement A1
NESTED IF STATEMENTS
START
Ex 4.6
program MultipleSelection;
var
conduct : integer;
Begin
write(‘Enter conduct: ‘);
readln(conduct);
if conduct < 50 then
writeln(‘Poor’)
else
if conduct < 70 then
writeln(‘Satisfactory’)
else
if conduct < 90 then
writeln(‘Good’)
else
writeln(‘Excellent’)
display “Enter conduct”
input conduct
false
true
Conduct < 50
display “Poor”
false
Conduct < 70?
true
display “satisfactory”
false
Conduct < 90?
display “good”
true
display “excellent”
end.
END
WORKSHEET-CH 4
Q1 (a) Draw a flowchart for the following program.
(b) Rewrite the following program as
(i) one IF-THEN-ELSE statement
(ii) two IF-THEN statement
PROGRAM MC;
VAR
ans : INTEGER;
BEGIN
WRITELN(‘How many days in a week?’);
WRITELN(‘1. six 2. seven 3. eight’);
READLN(ans);
IF ans = 1 then
writeln(‘wrong’);
IF ans = 2 then
writeln(‘right’);
IF ans = 3 then
writeln(‘wrong’);
END.
START
display “How many days in a week”
1. six 2 seven 3. eight
input choice
Choice =1?
false
Choice=2?
false
Choice=3?
false
true
output ‘wrong’
true
output ‘right’
true
output ‘wrong’
END
(c) Modify the above program to enter A, B,C as choice instead of integers.
Output screen:
How many days in a week?
A. six B. seven C. eight
B
right
b(i)
IF .. THEN
program mc;
var
ans : integer;
begin
writeln(‘how many days in a week?’);
writeln(‘1. six 2. seven 3. eight’);
readln(ans);
if (ans = 1) or (ans=3) then
writeln(‘wrong’);
if ans = 2 then
writeln(‘right’);
end.
b(ii)
program mc;
var
ans : integer;
begin
writeln(‘how many days in a week?’);
writeln(‘1. six 2. seven 3. eight’);
readln(ans);
if (ans = 1) or (ans=3) then
writeln(‘wrong’)
else
writeln(‘right’);
end.
b(ii)
c)
program mc;
var
ans :char;
begin
writeln(‘how many days in a week?’);
writeln(‘A. six B. seven C. eight’);
readln(ans);
if (ans = ‘A’) or (ans=‘C’) then
writeln(‘wrong’)
else
writeln(‘right’);
end.
IF –THEN-ELSE
IF –THEN-ELSE
program mc;
var
ans : integer;
begin
writeln(‘how many days in a week?’);
writeln(‘1. six 2. seven 3. eight’);
readln(ans);
if ans = 2 then
writeln(‘right’)
else
writeln(‘wrong’);
end.
Q2
(a) Draw a flowchart for the following program.
(a)
START
(b) Rewrite the following program as IF-THEN
program yesorno;
‘are there7days in a week?’
var
ans : char;
Input answer
begin
writeln(‘are there 7 days in a week? (y/n)’);
false
true
Answer=Yes?
(b)
readln(ans);
if ans = ‘y’ then
if ans = ‘y’ then
display “right”
Display ‘wrong’
writeln(‘right’)
writeln(‘right’);
else
if ans = ‘n’ then
END
writeln(‘wrong’);
writeln(‘wrong’);
end.
(c) program EnterPassword;
(c) Write a program to enter a password
var
and check if it is valid.
password : string;
begin
Output screen:
writeln(‘Enter your password’);
Enter your password?
readln(password);
1st run teacher
if password = ‘student’ then
Wrong!
writeln(‘Correct’)
else
Enter your password?
2nd run student
writeln(‘Wrong’);
end.
Correct!
Q3 Use the following program (find the area of circle) as a reference
program circlearea;
const
pi = 3.1416;
var
r : real;
begin
writeln(‘enter radius:’);
readln(r);
if r > 0 then
writeln(‘circle area is ’, pi*r*r : 0:2)
else
writeln(‘radius can not be < =0’);
end.
Write a program that asks you to enter length
in km and display it length in metre.
Output screen:
1st run
Enter length in km?
0
Length must be > 0 !
2nd run
Enter length in km?
3
The length is 3000 m
program convertkm;
var
km : real;
begin
writeln(‘Enter length in km?’);
readln(km);
if km > 0 then
writeln(‘The length is ’, km*1000: 0:2, ‘m’)
else
writeln(‘Length must be > 0!’);
end.
START
Q4 (a) Draw a flowchart for the following program.
(b) Rewrite the following program as IF-THEN
Ask for 2 numbers’
Input 2 numbers x, y
program choiceofcalculation;
var
choice : char;
x, y, z. : integer;
Input ‘+’ or ‘-’
false
begin
writeln(‘enter two numbers’);
readln(x, y);
writeln(‘add or multiply? + or -’);
readln(choice);
if choice = ‘+’ then
begin
z := x + y;
writeln(‘the sum is ’, z);
end
else
end.
Adding or subtract the numbers?
choice =‘+’?
z=x-y
Display the subtraction
true
z=x+y
Display the sum
if choice = ‘+’ then
END
begin
z := x + y;
writeln(‘the sum is ’, z);
end;
begin
if choice = ‘-’ then
z := x - y;
writeln(‘the difference is’, z); begin
z := x - y;
end;
writeln(‘the difference is’, z);
end;
Q5 Fill in the blanks of the following program to validate the two sides
PROGRAM PythTheorem;
VAR
A,B : real;
BEGIN
WRITELN(‘Enter side A for a right-angled triangle :’);
READLN(A, B);
IF (A > 0) and (B > 0) THEN
WRITELN(‘Hypothuse is ’, sqrt(A*A+B*B))
ELSE
WRITELN(‘Length of sides must be >0’);
END.
Q6 The following program asks you to enter a month and display the number of days for that
month. Rewrite the following program as (a) IF-THEN, (b) IF-THEN-ELSE (c) CASE
(a)
program calender;
program calender;
var
var
m : integer;
m : integer;
begin
begin
writeln(‘enter a month.’);
writeln(‘enter a month.’);
readln(m);
readln(m);
writeln(‘no. of days in the month is ’);
writeln(‘no. of days in the month is ’);
if m=1 then
if (m=1) or (m=3) or (m=5) or (m=7) or (m=8) or (m-10) or (m=12) then
writeln(31);
writeln(31);
if m=3 then
if m=2 then
writeln(31);
writeln(28);
if m=5 then
if (m=4) or (m=6) or (m=9) or (m=11) then
writeln(31);
if m=7 then
writeln(30);
writeln(31);
end.
if m=8 then
(b)
writeln(31);
program calender;
if m=10 then
var
writeln(31);
m : integer;
if m=12 then
begin
writeln(31);
writeln(‘enter a month.’);
if m=2 then
writeln(28);
readln(m);
if m=4 then
writeln(‘no. of days in the month is ’);
writeln(30);
if (m=1) or (m=3) or (m=5) or (m=7) or (m=8) or (m-10) or (m=12) then
if m=6 then
writeln(31)
writeln(30);
else
if m=9 then
if m=2 then
writeln(30);
writeln(28)
if m=11 then
else
writeln(30);
writeln(30);
end.
Q6 The following program asks you to enter a month and display the number of days for that
month. Rewrite the following program as (a) IF-THEN, (b) IF-THEN-ELSE (c) CASE
program calender;
var
m : integer;
begin
writeln(‘enter a month.’);
readln(m);
writeln(‘no. of days in the month is ’);
if m=1 then
writeln(31);
if m=3 then
writeln(31);
if m=5 then
writeln(31);
if m=7 then
writeln(31);
if m=8 then
writeln(31);
if m=10 then
writeln(31);
if m=12 then
writeln(31);
if m=2 then
writeln(28);
if m=4 then
writeln(30);
if m=6 then
writeln(30);
if m=9 then
writeln(30);
if m=11 then
writeln(30);
end.
(c)
program calender;
var
m : integer;
begin
writeln(‘enter a month.’);
readln(m);
writeln(‘no. of days in the month is ’);
case m of
1,3,5,7,8,10,12
: writeln(31);
2
: writeln(28);
4,6,911
: writeln(30)
end
end.
Q7
The program randomly generate two numbers and ask you to guess if first number
is greater than the second, the second number is greater than the first number or
both are equal to each other. Finally the program display your guesses status.
Output screen:
Generating 2 number now
Enter your choice:
1. first number > second number
2. Second number > first number
3. First number = second number
2
Correct.
The first number is 5
The second number is 8
Q8 The following program generate a number from 1 to 48 , ask you to enter your number
and display if you guess right or wrong.
program marksix;
var
num, ans : integer;
begin
randomize;
num := random(48)+1;
writeln(‘enter a lotto number’);
readln(ans);
if ans = num then
writeln(‘win’)
else
writeln(‘loss’)
end.
Q 8 Refer to the above program. Write a program that
1. generates 3 numbers randomly (representing results of 3 dice throws),
2. ask user to guess Big or Small.
‘Big’ if sum of the 3 numbers is greater than 10.
‘Small’ if the sum is less than or equal to 10.
3. Display the message about if the user wins or not.
Q9:
Write a program that asks you to enter 3 sides of a triangle and then check if it is
a right angle triangle.
A,B and C are variables for the three sides of a triangle.
ASQ, BSQ, CSQ are variables for the squares of the three sides.
PROGRAM TestRightAngledTriangle;
VAR
A,B,C,ASQ,BSQ,CSQ : integer;
BEGIN
writeln(‘Enter 3 sides of a triangle’);
readln(A,B,C);
ASQ := A*A;
BSQ := B*B;
CSQ := C*C;
if CSQ=ASQ+BSQ then
writeln(‘IT is a right-angled triangle’)
else
writeln(‘It is not a right-angled triangle’);
END.
Q10.
Write a program that
accepts coordinates of 2 points on a line,
check if the slope is undefined (Ax=Bx). Otherwise calculate the slope of line passing through these
two points.
A (Ax,Ay)
Let Ax,Ay be the variables for the coordinates of point A
Let Bx, By be the variables for the coordinates of point B.
Let m be the slope of the line passing through the two points A and B.
mAB 
Ay  By
Ax  Bx
program slope;
B (Bx,By)
var
m, Ax,Ay, Bx,By: real;
begin
writeln(‘Enter the coordinate of point A’);
readln(Ax,Ay);
writeln(‘Enter the coordinate of point B’);
readln (Bx,By);
if Ax=Bx then
writeln(‘slope is undefined’)
else
begin
m := (Ay-By)/(Ax-Bx) ;
writeln(‘the slope of line passing through points is ’, m:0:2)
end
end.
Worksheet
Q1Fill in the blanks. The program ask you to enter a uppercase letter and convert it into
lowercase
PROGRAM CONVERTTOLOWERCASE;
VAR
Ch : char ;
BEGIN
READLN(Ch);
IF (Ch >= 'A') and (Ch <= ‘Z’ ) then
WRITELN(CHR(ord(Ch)+32));
END.
Letter
ASCII
number
Letter
ASCII
number
A
65
a
97
B
66
b
98
C
67
c
99
.
.
.
.
.
.
..
.
Z
90
z
122
Assignment
To solve a quadratic equation Ax2 + Bx + C = 0, we use the formula.
 B  B 2  4 AC
x
2A
or
 B  B 2  4 AC
x
2A
Write a program that
input 3 coefficients
Check if coefficient A is zero or not.
If yes, stop the program.
Otherwise
check if B2-4AC < 0, display “no real roots” message.
check if B2-4AC = 0, display one real root.
check if B2-4AC > 0, display the two real roots.
First Run:
Enter coefficients A, B and C for Ax*x+Bx+C=0: 2 5 -3
The real roots are -3 and 0.5
Second Run:
Enter coefficients A, B and C for Ax*x+Bx+C=0: 1 3 3
No reat roots!
Third Run:
Enter coefficients A, B and C for Ax*x+Bx+C=0: 4 4 1
The real root is -0.5
program quadratic;
var
a,b,c,d : real;
begin
writeln('enter coefficients a, b and c for the the qradratic equation ax*x+bx+c=0');
readln(a,b,c);
if a <> 0 then
begin
d := b*b-4*a*c;
if d = 0 then
writeln('the real root is ', -b/(2*a):0:1 );
if d > 0 then
writeln('the roots are ',(-b+sqrt(d))/(2*a):0:1,' and ', (-b-sqrt(d))/(2*a):0:1);
if d < 0 then
writeln('no real roots!')
end
program quadratic;
var
a,b,c,d : real;
begin
writeln('enter coefficients a, b and c for the the qradratic equation ax*x+bx+c=0');
readln(a,b,c);
if a <> 0 then
begin
d := b*b-4*a*c;
if d = 0 then
writeln('the real root is ', -b/(2*a):0:1 )
else
if d > 0 then
writeln('the roots are ',(-b+sqrt(d))/(2*a):0:1,' and ', (-b-sqrt(d))/(2*a):0:1)
else
writeln('no real roots!')
end
Assignment
The following program draws a character
From A to E and asks user to guess.
If guess is correct, display correct
Otherwise, display incorrect
program drawcharacter;
var
ch, ans : char;
begin
writeln('guess a character from A to E ...');
readln(ans);
randomize;
writeln('drawing a character from A to E ...');
ch :=chr( 65+random(5));
if ch = ans then
writeln('correct')
else
writeln('incorrect');
writeln('the character drawn is ', ch)
end.
Write a program that draws a character
from  or .
User then guess the draw result.
If guess is correct, display correct
Otherwise, display incorrect
Output:
Enter 1. 
2
2. 3.
Drawing begins..
Correct. The result is : 
4.  to guess
Ex 4.7
Program MultipleSelection3;
Var
conduct : integer;
Begin
write(‘Enter your conduct’);
readln(conduct);
case Conduct of
0 .. 49: writeln(‘Poor’);
50-69: writeln(‘Satistifactory’);
70-89: writeln(‘Good’);
90-100: writeln(‘Excellent’)
end
End.
Ex 4.8
Program Calculator;
Var
X,Y : integer;
operator: char;
Begin
write(‘Enter 2 numbers’);
readln(X,Y);
write(‘Enter operator (+,-, * or /): ’);
readln(operator);
case operator of
‘+’: writeln(X+Y);
‘-’: writeln(X-Y);
‘*’: writeln(X*Y);
‘/’ : writeln(X/Y)
end
End.
Ex 4.8
Program Activities42;
Var
number : integer;
Begin
write(‘Enter a lucky number ’);
readln(number);
case number of
1
: writeln(‘you win a car!’)
2,3,4
: writeln(‘you win a diamound ring!’);
5,6,8,9,10: writeln(‘you win a golden ring!’);
11.. 20 : writeln(‘you win a pen’);
21..100 : writeln(‘Sorry!you have no prize’);
end
End.