Upload
alaula
View
21
Download
0
Embed Size (px)
DESCRIPTION
Definite Iteration statements execute a statement repeatedly for a definite number of times. FOR := to do . where must be of integer, char, boolean. Set I to 1. Set I to ‘A’. F. I
Citation preview
FOR <Control Variable> := <Initial Value> to <Final Value> do<Loop statement>
Definite Iteration statements execute a statement repeatedly for a definite number of times
where <Control Variable> must be of integer, char, boolean
Example:
Program Ex51;var
i : integer;begin
for i := 1 to 5 dowriteln(i);
end.
program Ex51;var
i : char;begin
for i := ‘A’ to ‘E’ dowriteln(i);
end.
I <= 5?
T
Display I
T
Increment I by 1
Set I to 1
F
I <= ‘E’?
T
Display I
T
Increment I by 1 character
Set I to ‘A’
F
program Ex1;var i : integer;begin for i := 1 to 5 do writeln(i)end.
program Ex2;Var
i: integer;begin
for i := 1 to 5 dobegin
write('The number is ');writeln(i)
endend.
program Ex3;var
i, mark: integer;begin
for i := 1 to 5 dobegin
write('Enter your marks ');readln(mark)
endend.
program Ex4;var i, mark, sum: integer;begin sum :=0; for i := 1 to 5 do begin
write('Enter your marks ');readln(mark);sum := sum + mark
end;writeln('The average is ', sum/5:0:2)
end.
program SumOfSeries;var i,N, total: integer;begin
writeln('Find 1+2+3+4+....+N');write('Enter the N th term:');
readln(N);
total := 0; for i := 1 to N do total := total + i;
writeln('1+2+3+4+5+....+',N, '=', total)end.
0 111 233 366 410
program SumOfSeriesForOddNumber;var i,N, total: integer;begin writeln('Find 1+3+5+7+....+N'); write('Enter the N th term:'); readln(N); total := 0; for i := 1 to round(N/2) do total := total + 2*i-1; writeln('1+3+5+7+....+',N,'=',total)end.
100
50
program powerofindex;var i,p,a : integer;begin
write('Enter power of index:'); readln(p); a := 1; for i := 1 to p do a := a * 2;
writeln('2 to the power ', p, '=', a)end.
p=3
23=8
a:= 1a:= a 22 1 24 2 28 4 2
i=1i=2i=3
2221 a
3
不斷將 2 自乘 p 次
執行 p 次 將 2 自乘
WorkSheetWrite a program that •accepts an integer N•And calculate the result of 1 2 3 …….N
Output:Enter a number : 5The result of 1 2 ……. 5 is: 120
program powerofindex;var i,p,a : integer;begin
write('Enter a number:'); readln(p); a := 1; for i := 1 to p do a := a * i;
writeln(‘1x2x…x’,p,‘ is ',a)end.
執行 p 次 將 i 乘以之前累積乘數
program investment;var I,N : integer; P,R,A : real;begin
write('Enter amount to be borrowed by Tom:');readln(P);write('Enter the yearly compound interest rate in %:');readln(R);write('Enter the number of years:');readln(N);write('How much does he owe after ', N, 'years? ');
A:=P;for I := 1 to N do
A := A * (1+R/100);
writeln(A:0:2)end.
)100
1(.......)100
1()100
1(RRR
PA
N
A 1000A:= A (1+R/100)1100 1000 1.11210 1100 1.11331 1210 1.1
I=1I=2I=3
P=1000
R=10
N=3
執行 N 次 將 本金不斷乘以 (1+R/100)
program SumOfOddnumbers;var i,N, total: integer;begin writeln('Find 1+3+5+7+....+N'); write('Enter the N th term:'); readln(N);
total := 0; for i := 1 to N do if i mod 2 <> 0 then total := total + i;
writeln('1+3+5+7+....+',N,'=',total)end.
Example 5.2
如果是單數 ( 不可除盡 2, 餘數 ; 非 0 )累積單數總和
Program sumOfOddandEven;var i,N, osum, esum: integer;begin writeln('Find 1+3+5+7+....+N');
writeln('Find 2+4+6+8+....+N'); write('Enter the N th term:'); readln(N);
osum := 0;esum:=0;
for i := 1 to N do if i mod 2 = 0 then esum := esum + I
elseosum := osum+I;
writeln('1+3+5+7+....+',N,'=',osum);writeln(‘2+4+6+8+....+',N,'=',esum);
end.
如果是雙數 ( 可除盡 2, 餘數 0 )累積雙數總和
否則累積單數總和
program FindFactors;var i,N: integer;begin
write('Enter a number:'); readln(N); write('The factors of ', N, ' are '); for i := 1 to N do if N mod i = 0 then write(i:5)end.
24
24
Output:The factors of 24 are 1 2 3 4 6 8 12 24
24 mod 1 = 024 mod 2 = 024 mod 3 = 024 mod 4 = 0
24 mod 6 = 024 mod 8 = 0
N mod i = 0
24 mod 12= 024 mod 24= 0
找出某數的所有因數
如 數值如24可以除盡 I 的話 ,即 I 是24的因數),
顯示因數
PROGRAM MONTH;VAR
I: INTEGER;BEGIN FOR I := 1 TO 30 DO BEGIN WRITE(I:3); IF I MOD 7 = 0 THEN WRITELN ENDEND.
1 2 3 4 5 6 78 9 10 11 12 13 14
15 16 17 18 19 20 2122 23 24 25 26 27 2829 30
Output:
如果 I 可以除盡 7 的話(即 I 是 7 的倍數)便落一行
program determinePrimeNumber;var number,i, count : integer;begin writeln('Enter a number'); readln(number);
count := 0; for i := 1 to number do if number mod i = 0 then
count := count + 1;
if count = 2 then writeln(number, ' is a prime number.') else writeln(number, ' is not a prime number.')end.
6
6 mod 1 = 06 mod 2 = 0 6 mod 3 = 06 mod 6 = 0
count = 1count = 2count = 3count = 4
3
3 mod 1 = 03 mod 3 = 0
count = 1count = 2
如 數值如 6 可以除盡 I 的話 , 即 I 是數值的因數)因數數量便加一
如因數數量只有二 ,數值是一個質數
program findhcf;var num1, num2, l,i, hcf:integer;begin writeln('please enter two numbers'); readln(num1,num2); if num1 > num2 then l := num2 else l := num1;
for i := 1 to l do
if (num1 mod i = 0) and (num2 mod i = 0) then hcf := i;
writeln('the hcf is ', hcf)end.
9 63
L=9
9 mod 3=0 63 mod 3=0
那個數目最少用 L 載著
找出最少公因數
找出兩個數的公因數I
Write a program that accepts marks for 8 students and find the highest mark.
Output:
Enter mark : 40Enter mark : 45Enter mark : 65Enter mark : 87Enter mark : 75Enter mark : 89Enter mark : 34Enter mark : 32
The highest mark is 87
program FindTheHighestMark;var mark, i, hmark: integer;begin
write(‘Enter mark:’);readln(mark);
hmark:=mark; for i := 2 to 8 do begin readln(mark); if mark >= hmark then hmark := mark; end; writeln('The highest mark is ', hmark)end.
設第一個分數最大由第二個次到第八個次
每次輸入分數如分數大過之前最大數
設分數為最大
找出輸入數值中的最大數
program reverseword;var w,c: string; i : integer;begin writeln('Enter a word'); readln(w);
c:=‘’ ; for i := 1 to length(w) do c := w[i] + c; writeln('The reversed word is ', c)end.
w:= ‘abcd’
4c w[i]+c‘a’ ‘a’+‘’ ‘ba’ ‘b’+‘a’‘cba’ ‘c’+‘ba’‘dcba’ ‘d’+‘cba’
每個字母與之前的字串合併
把輸入的句子由尾反轉顯示
從第一個字母到最後一個字母
PROGRAM REVERSE;VAR W : STRING; I : INTEGER;BEGIN WRITE('ENTER A SENTENCE:'); READLN(W); FOR I := 1 TO LENGTH(W) DO
WRITE(W[LENGTH(W)+1-I])END.
OUR LADY
8
9-1=89-2=79-3=69-4=59-5=49-6=39-7=29-8=1
program findcharacter;var c : char; w : string; i, found : integer;begin write('enter a letter to search from the sentence:'); readln(c); w := 'school';
found := 0; for i := 1 to length(w) do
if w[i] = c then found := 1;
if found = 1 then writeln('character found. The sentence is ’, w) else writeln('character not found! The sentence is ’, w ); end.
輸入一個字母
檢查字母是否在句子裏
一旦發現字母在句子裏設定找到
開始時設定找不到
WorkSheetwrite a program that accepts a string and change each uppercase letter to lowercase letter.
Enter your school name :I studied from Our Lady CollegeThe result is : i studied from our lady college
Output:
program uppertolowercase;var
w: string;i : integer;
beginwriteln('enter your school name:');readln(w);write(‘The result is’);for i := 1 to length(w) do
if (w[i]>=‘A') and (w[i] <=‘Z') thenwrite(chr(ord(w[i])+32))
else write(w[i])end.
測試句子內每一個字符如果是英文字母大楷
顯示細楷否則
顯示原本字符
WorkSheetwrite a program that accepts a string and change each lowercase letter to uppercase or vice versa.
Enter your school name :50th Anniversary Our Lady CollegeThe result is : 50TH aNNIVERSARY oUR lADY cOLLEGE
Output:
program convertcase;var
w: string;i : integer;
beginwriteln('enter your school name:');readln(w);write(‘The result is’);for i := 1 to length(w) do
if (w[i]>=‘A') and (w[i] <=‘Z') thenwrite(chr(ord(w[i])+32))
elseif (w[i]>=‘a') and (w[i] <=‘z') then
write(chr(ord(w[i])-32))else
write(w[i])end.
測試句子內每一個字符如果字符是英文字母大楷
顯示細楷否則
如果是英文字母細楷顯示大楷
否則 顯示原本字符
WorkSheet
Output:Enter a sentence : Computer Information TechnologyThe number of occurrence for the vowels :10
Write a program that •accepts a sentence•And calculate the number of occurrences for the vowels.
Computer Information Technologyprogram countvowels;var
w: string;i,c : integer;
begin writeln('Enter a sentence'); readln(w); c:=0 ; for i := 1 to length(w) do
case w[i] of‘A’,’a’,’E’,’e’,’I’,’i’,’O’,’o’,’U’,’u’: c := c +1;
end;writeln(‘The number of occurrence for the vowels:’, c)
end.
測試句子內每一個字符如果字符是韻母
將韻母數目加一
WorkSheetwrite a program that accepts a sentence and display the first letter of each word
First Run:Enter your school name :OurLadyCollegeYour school is : OLC
Output:
Second Run:Enter your school name :OurLadyCollegeYour school is : OLC
program extractfirstletter;var c : string;
i: integer;begin readln(c); for i := 1 to (length(c)-1) do begin if (i = 1) and (c[i] <> ' ') then write(c[i]); if (c[i]=' ') and (c[i+1]<>' ') then write(c[i+1]) endend.
顯示句子內每一個字的首字母
如果這是空字符和下一個是有字符顯示下一個字符
如果這是第一個字符而不是空字符顯示這個字符
測試每一個句子內每一個字符
PROGRAM CONVERTTOLOWERCASEORUPPERCASE;VAR S : STRING; I : INTEGER;
PROCEDURE CONVERT(VAR X: CHAR);BEGIN IF (X >= 'A') AND (X <= 'Z') then X := CHR(ORD(X)+32);
IF (X >= 'a') AND (X <= 'z') then X := CHR(ORD(X)-32);END;
BEGIN WRITELN('ENTER A SENTENCE:'); READLN(S); CONVERT(S[1]); FOR I := 1 TO LENGTH(S)-1 DO IF (S[I] = ' ') AND (S[I+1] <> ' ') THEN CONVERT(S[I+1]); WRITELN('THE SENTENCE NOW IS ', S)END.
program pattern1;var c : string; i : integer;begin
c := '';for i := 1 to 4 dobegin
c := c + '*'; writeln(c) end;end.
**********
program pattern2;var
c : string;i : integer;
beginc := '';for i := 1 to 4 do
begin c := c + '*'; writeln(c:4) end;end.
***
*******
c= ‘’c= c+’*’‘*’ ‘’+‘*’ ‘**’ ‘*’+ ‘*’‘***’ ‘**’+ ‘*’ ‘****’ ‘***’+‘*’
Nest-FORprogram MultiplyTable;var I,J : integer;begin for I := 1 to 10 do begin for J := 1 to 10 do write(I * J :4); writeln endend.
100908070
605040
30
20
10
10
90817263
544536
27
18
99
80726456
484032
24
16
88
70635649
423528
21
14
77
60544842
363024
18
12
66
50454035
302520
15
10
55
40363228
242016
12
844
30272421
181512
9633
20181614
121086422
109876543211
10987654321X
I
J
Nest-FORprogram pattern;var i,j : integer;begin for i := 1 to 4 do begin for j := 1 to i do write('*'); writeln endend.
program pattern;var i,j : integer;begin for i := 4 downto 1 do begin for j := 1 to i do write('*'); writeln endend.
* ******** *
1i
2
3
4
1
2
34
j
**********
4i
3
2
1
4
3
2
1
j
Nest-FORprogram pattern;var i,j : integer;begin for i := 1 to 4 do begin write('':4-i); for j := 1 to i do write('*'); writeln endend.
program pattern;var i,j : integer;begin for i := 4 downto 1 do begin write('':4-i); for j := 1 to i do write('*'); writeln endend.
**** **** **
1i
2
3
4
3
2
10
4-i* **** *** **
4
i
3
2
1
0
1
2
3
4-i
WorkSheet Q1
Using for statement with if-statement within , write a program that generates the following output:
|---|---|---|---||---|---|---|---||---|---|---|---|
Using nested-for , write a program that generates the following output:
|---|---|---|---||---|---|---|---||---|---|---|---|
WorkSheet Q2
PROGRAM PATTERN;VAR
I : INTEGER;BEGIN FOR I := 1 TO 12 DO BEGIN WRITE(‘|---’); IF I MOD 4 = 0 THEN WRITELN(‘|’) ENDEND.
PROGRAM PATTERN;VAR
I ,J: INTEGER;BEGIN FOR I := 1 TO 3 DO BEGIN FOR J := 1 TO 4 DO
WRITE(‘|---’); WRITELN(‘|’) ENDEND.
連續顯示12次 |---
每四次落一行
顯示三行
每一行顯示四次 |---
WorkSheet Q2
Denary Number = a25 + b23 +c23 + d22 + e21+f 20
Where a,b,c,d,e are digits of either 0 or 1.
Write a program that accepts a series of 1’s and 0’s. and display the denary number.
Output:
Enter binary number:10111
The denary number is 23
program DenaryToBinary;var c : string; i,j,sum, a,m: integer;begin writeln(‘Enter binary number’); sum := 0; for i := 5 downto 2 do
begin readln(a); m := 1; for j := 1 to (i-1) do
m := m * 2; sum := sum+(a*m); end;
readln(a); sum := sum + a; writeln(‘The denary number is’, sum)end.
ProjectWrite a program that •display the result of days for each month in the following format:
Month 11 2 3 4 5 6 78 9 10 11 12 13 14
15 16 17 18 19 20 2122 23 24 25 26 27 2829 30 31
Output:
Month 21 2 3 4 5 6 78 9 10 11 12 13 14
15 16 17 18 19 20 2122 23 24 25 26 27 2829
.
.
.Month 12
1 2 3 4 5 6 78 9 10 11 12 13 14
15 16 17 18 19 20 2122 23 24 25 26 27 2829 30 31
program Ex1;var i : integer;begin for i := 1 to 5 do writeln(i)end.
program Ex3;var
i, mark: integer;begin
for i := 1 to 5 dobegin
write('Enter your marks ');readln(mark)
endend.
DIFFERENCE BETWEEN FOR AND REPEAT LOOPprogram Ex1;var i : integer;Begin
i := 0;repeat
i := i + 1; writeln(i);
until i = 5;end.
program Ex3;var
i, mark: integer;Begin
i := 0;repeat
i := i +1;write('Enter your marks ');readln(mark);
until i = 5;end.
program Ex4;var i, mark, sum: integer;begin sum :=0; for i := 1 to 5 do begin
write('Enter your marks ');readln(mark);sum := sum + mark
end;writeln('The average is ', sum/5:0:2)
end.
program Ex4;var i, mark, sum: integer;begin sum :=0; i := 0; repeat
i:=i +1;write('Enter your marks ');readln(mark);sum := sum + mark
until i = 5;writeln('The average is ', sum/5:0:2)
end.
program SumOfSeries;var i,N, total: integer;begin
writeln('Find 1+2+3+4+....+N');write('Enter the N th term:');
readln(N);
total := 0; for i := 1 to N do total := total + i; writeln('1+2+3+4+5+....+',N, '=', total)end.
program SumOfSeries;var i,N, total: integer;begin
writeln('Find 1+2+3+4+....+N');write('Enter the N th term:');
readln(N);i :=0;
total := 0; repeat
i :=i + 1; total := total + i;
until i = N; writeln('1+2+3+4+5+....+',N, '=', total)end.
REPEAT
statement 1;statement2;
.
.UNTIL condition;
program enteryourage;var
age : integer;begin
repeatwriteln(‘enter your age:’);readln(age);
until age > 12;end.VALIDATION INPUT TECHNIQUE
program enteryourage;var
age : integer;begin
repeatwriteln(‘enter your age:’);readln(age);
until (age > 12) and (age <15);end.
program atm;var
key : integer;begin
repeatwriteln(‘enter password:’);readln(key);
until key = 12345;end.
VALIDATION INPUT TECHNIQUE
program atm;var
key, c : integer;begin
c := 0;repeat
writeln(‘enter password:’);readln(key);c := c + 1;
until (key = 12345) or (c = 3) ;
if key = 12345 then writeln(‘correct. Go in’)
elsewriteln(‘please leave’)
end.
VALIDATION INPUT AND COUNTING TECHNIQUE
program findtheterm;var
term, sum : integer;begin
writeln(‘the term n where ‘1+2+3+..+n >12 is ’);term := 0;sum :=0;repeat
term := term + 1; sum := sum + term;
until sum = > 12;writeln(term)
end.
COUNTING AND ACCUMULATING TECHNIQUE
term=0term =term +11 =0 +12 =1 +13 =2 +14 =3 +15 =4 +1
sum=0sum =sum+term1 =0 +13 =1 +26 =3 +310 =6 +415 =10 +5
program FindRemainder;var
d, num : integer;begin
num:=9;d :=2;
repeatnum := num - d;
until num < d;writeln('the remainder is ' , num);
end.
num =num-d7 =9 -25 =7 -23 =5 -21 =3 -2
COUNTING AND ACCUMULATING TECHNIQUE
program compoundinterest;var
p : real;yr : integer;
beginp:=10000;yr:=0;repeat p := p*1.1; yr := yr + 1;until p > 20000;writeln(‘It take ’,yr, ‘years to have an amount over $20000’);
End.
program findcharacter;var c : char; w : string; i, found : integer;begin write('enter a letter to search from the sentence:'); readln(c); w := 'school';
found := 0; for i := 1 to length(w) do
if w[i] = c then found := 1;
if found = 1 then writeln('character found. The sentence is ’, w) else writeln('character not found! The sentence is ’, w ); end.
program findcharacter;var c : char; w : string; i : integer;begin writeln('enter a letter to search from the sentence:'); readln(c); w := 'school';
i := 0; repeat i := i + 1 until (w[i] = c) or (i = length(w));
if w[i]= c then writeln('character found. The sentence is ', w) else writeln('character not found! The sentence is ', w);end.
program findcharacter;var c : char; w : string; i : integer;begin writeln('enter a letter to search from the sentence:'); readln(c); w := 'school';
i := 0; while (w[i] <> c) and (i <> length(w)) do i := i + 1;
if w[i]= c then writeln('character found. The sentence is ', w) else writeln('character not found! The sentence is ', w);end.
program atm;var amt,money,choice,count : real;
password: string;ans : char;
begincount := 0;repeat
writeln('Enter your password'); readln(password); count := count + 1 until (password='OLC') or (count=3); if password='OLC' then begin repeat
write('Enter your saving balance:'); readln(amt) until amt >0 ; repeat writeln('1. enquiry 2. withdraw 3. deposit 4. exit'); readln(choice); if choice = 1 then
writeln('your balance is ', amt:0:2); if choice = 2 then
begin repeat write('enter amount to withdraw: '); readln(money) until money < amt; amt := amt-money;
end; if choice = 3 then begin write('enter amount to deposit '); readln(money); amt := amt + money
end; write('continue?Y/N '); readln(ans) until (ans = 'N') or (ans='n'); end else writeln('Three times has passed')end.
program guessgames;var num, guess, turn: integer;begin turn:= 1; repeat turn := turn+1; randomize; num := random(5)+1; writeln('Player ', turn mod 2 + 1,', guess a number from 1 to 5'); readln(guess); until guess = num; writeln('The number drawn is ', num); writeln('Player ', turn mod 2+1, ' win')end.
program guessgames;var num, guess, turn, player: integer;begin turn:= 1; repeat randomize; num := random(5)+1; if turn mod 2 = 1 then player := 1 else player := 2; writeln('Player ', player, '.Guess a number from 1 to 5'); readln(guess); turn := turn + 1; until guess = num; writeln('The number drawn is ', num,'.Player ', player, ' win')end.
program guessgames;var
turn, player: integer; num, guess : char;begin turn:= 1; repeat randomize; num := chr(65+random(5)); if turn mod 2 = 1 then player := 1 else player := 2; writeln('Player ', player, '.Guess a character from A to E'); readln(guess); turn := turn + 1; until guess = num; writeln('The character drawn is ', num,'.Player ', player, ' win')end.
program guess;var num1, num2, pts : integer; a, b,ans: char;begin pts := 500; writeln('your points are initially ', pts); repeat randomize; num1 := random(10)+1; num2 := random(10)+1; if num2 > num1 then a := '>‘; if num2 < num1 then
a :='<‘; if num2 = num1 then a :='='; writeln('guess if 1st letter > < or = 2nd letter'); readln(b); write('numbers are ',num1,' and ', num2); if a = b then pts := pts+100 else pts := pts-100; if pts >= 0 then begin write(' your points are ', pts,'.continue? y or n'); readln(ans) end until (pts < 0) or (ans = 'n'); if pts < 0 then writeln('.you pts are less than zero! you are not allowed to play!');end.
program MarkSix;const max = 20;var a,b,c,d,e,f,g,h,i,j,k,l, count: integer;
Begin
repeatreadln(g)
until (g > 0) and (g <=max); repeat
readln(h) until (h > 0) and (h <=max) and (h <> g); repeat
readln(i) until (i > 0) and (i <=max) and (i <> g) and (i <> h); repeat
readln(j) until (j > 0) and (j <=max) and (j <> g) and (j<> h) and (j<> i); repeat
readln(k) until (k > 0) and (k <=max) and (k <> g) and (k<> h) and (k<> i) and (k<>j); repeat
readln(l) until (l > 0) and (l <=max) and (l <> g) and (l<> h) and (l<> i) and (l<>j) and (l <> k); writeln('Your numbers are ', g:3, h:3, i:3, j:3, k:3, l:3);
randomize; a := random(max)+1; repeat b := random(max)+1 until b <> a; repeat c := random(max)+1 until (c <> a) and (c <> b); repeat d := random(max)+1 until (d<>a) and (d <>b) and (d <> c); repeat e := random(max)+1 until (e<>a) and (e <> b) and (e <> c) and (e <> d); repeat f := random(max)+1; until (f <> a) and (f <> b) and (f <> c) and (f<>d) and (f <> e); writeln('Number generated by computer are ', a:3, b:3, c:3, d:3, e:3, f:3);
count := 0; if (g=a) or (g=b) or (g=c) or (g=d) or (g=e) or (g=f) then
count := count+1;
if (h=a) or (h=b) or (h=c) or (h=d) or (h=e) or (h=f) thencount := count+1;
if (i=a) or (i=b) or (i=c) or (i=d) or (i=e) or (i=f) thencount := count+1;
if (j=a) or (j=b) or (j=c) or (j=d) or (j=e) or (j=f) thencount := count+1;
if (k=a) or (k=b) or (k=c) or (k=d) or (k=e) or (k=f) thencount := count+1;
if (l=a) or (l=b) or (l=c) or (l=d) or (l=e) or (l=f) thencount := count+1;
case count of0..2 : writeln(count,' match.No price!');
3 : writeln('fourth price');4 : writeln('third price $20');5 : writeln('second price');6 : writeln('First price')
endend.
Write a program that keeps generating a number of either 0 or 1 randomly for you to guess until your guess is wrong. As long as each guess is correct, your money is increased by $1000.
guess a number 1 or 2: 1Correct. Your money so far is $1000
guess a number 1 or 2: 2Correct. Your money so far is $2000
guess a number 1 or 2: 1Wrong! The number generated is 2.Your money now is $2000
Output:
Write a program that keeps generating two numbers 1 and 2 randomly for you to guess if the second number is greater than first number, less than the first number and equal to the first number until your guess is wrong. As long as each guess is correct, your money is increased by $1000.
First number Greater Than(G), Less Than(L) or Equal To(E) the second number?GCorrect. The numbers are 1 and 2. You have gained 1000 points.
First number Greater Than(G), Less Than(L) or Equal To(E) the second number? LCorrect. The numbers are 2 and 1. You have gained 2000 points.
First number Greater Than(G), Less Than(L) or Equal To(E) the second number? ECorrect. The numbers are 2 and 2. You have gained 3000 points.
First number Greater Than(G), Less Than(L) or Equal To(E) the second number? LIncorrect! The numbers are 1 and 2. You have gained 3000 points.
program guessgames;var pts,num1, num2: integer; result, guess : char;begin pts:= 0; repeat randomize; num1 := random(2)+1; num2 := random(2)+1;
if num1 = num2 then result := 'E' else if num2 > num1 then result := 'G' else result := 'L';
writeln('First number Greater Than(G), Less Than(L) or Equal To(E) the second number?'); readln(guess); if guess = result then begin writeln('Correct.'); pts := pts + 1000; end else write('Incorrect!');
writeln('The numbers are ', num1, ' and ', num2, ‘.You have gained ', pts, ' points') until guess <> result;end.
PROGRAM HANGMAN;VAR W,D: STRING; E:CHAR; F,I,J,K,WRONG : INTEGER;BEGIN K := 0; WRONG := 0; D:='******'; W:='SCHOOL'; REPEAT WRITELN(D); WRITE('GUESS A LETTER FROM THE WORD:'); READLN(E);
I := 0; REPEAT I := I +1; UNTIL (D[I]=E) OR (I=LENGTH(D)); IF I=LENGTH(D) THEN BEGIN F := 0; FOR J := 1 TO LENGTH(W) DO IF W[J] = E THEN BEGIN F :=1; K:=K+1; D[J] := E; END; IF F=0 THEN BEGIN WRONG := WRONG+1; WRITELN(WRONG, ' TIMES WRONG') END END; UNTIL (WRONG = 10) OR (K=6); WRITELN( D); IF WRONG = 10 THEN WRITELN('YOU ARE OUT!') ELSE WRITELN('CONGRADUATION! YOU WIN')END.
program Guessing;var money,num,ans : integer;begin money := 0; repeat randomize; num:=random(2)+1; writeln('guess a number 1 or 2:'); repeat readln(ans) until (ans = 1) or (ans=2); if ans = num then begin money := money+1000; writeln(‘Correct.Your money so far is $', money) end until ans <> num; writeln(‘Wrong! Number generated is ',num);
writeln(‘Your money now is $',money)end.