Presentation is loading. Please wait.

Presentation is loading. Please wait.

FOR := to do Definite Iteration statements execute a statement repeatedly for a definite number of times where must be of integer, char, boolean.

Similar presentations


Presentation on theme: "FOR := to do Definite Iteration statements execute a statement repeatedly for a definite number of times where must be of integer, char, boolean."— Presentation transcript:

1 FOR := to do Definite Iteration statements execute a statement repeatedly for a definite number of times where must be of integer, char, boolean

2 Example: Program Ex51; var i : integer; begin for i := 1 to 5 do writeln(i); end. program Ex51; var i : char; begin for i := ‘A’ to ‘E’ do writeln(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

3 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 do begin write('The number is '); writeln(i) end end. program Ex3; var i, mark: integer; begin for i := 1 to 5 do begin write('Enter your marks '); readln(mark) end 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.

4 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. 01 1 1 2 3 336 6 4 10

5 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

6 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 2 3 =8 a:=1 a:=a  2 21  2 42  2 84  2 i=1 i=2 i=3 3 不斷將 2 自乘 p 次 執行 p 次 將 2 自乘

7 WorkSheet Write a program that accepts an integer N And calculate the result of 1  2  3  …….N Output: Enter a number : 5 The 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 乘以之前累積乘數

8 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. N A1000 A:=A  (1+R/100) 11001000  1.1 12101100  1.1 13311210  1.1 I=1 I=2 I=3 P=1000 R=10 N=3 執行 N 次 將 本金不斷乘以 (1+R/100)

9 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 ) 累積單數總和

10 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 else osum := osum+I; writeln('1+3+5+7+....+',N,'=',osum); writeln(‘2+4+6+8+....+',N,'=',esum); end. 如果是雙數 ( 可除盡 2, 餘數 0 ) 累積雙數總和 否則 累積單數總和

11 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 Output: The factors of 24 are1234681224 24 mod 1 = 0 24 mod 2 = 0 24 mod 3 = 0 24 mod 4 = 0 24 mod 6 = 0 24 mod 8 = 0 N mod i = 0 24 mod 12= 0 24 mod 24= 0 找出某數的所有因數 如 數值如24可以除盡 I 的話, 即 I 是24的因數), 顯示因數

12 PROGRAM MONTH; VAR I: INTEGER; BEGIN FOR I := 1 TO 30 DO BEGIN WRITE(I:3); IF I MOD 7 = 0 THEN WRITELN END END. 1234567 891011121314 15161718192021 22232425262728 2930 Output: 如果 I 可以除盡 7 的話(即 I 是 7 的倍數) 便落一行

13 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 = 0 6 mod 2 = 0 6 mod 3 = 0 6 mod 6 = 0 count = 1 count = 2 count = 3 count = 4 3 3 mod 1 = 0 3 mod 3 = 0 count = 1 count = 2 如 數值如 6 可以除盡 I 的話, 即 I 是數值的因數) 因數數量便加一 如因數數量只有二, 數值是一個質數

14 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. 963 L=9 9 mod 3=063 mod 3=0 那個數目最少用 L 載著 找出最少公因數 找出兩個數的公因數I

15 Write a program that accepts marks for 8 students and find the highest mark. Output: Enter mark : 40 Enter mark : 45 Enter mark : 65 Enter mark : 87 Enter mark : 75 Enter mark : 89 Enter mark : 34 Enter 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. 設第一個分數最大 由第二個次到第八個次 每次輸入分數 如分數大過之前最大數 設分數為最大 找出輸入數值中的最大數

16 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’ 4 cw[i]+c ‘a’‘a’+‘’ ‘ba’‘b’+‘a’ ‘cba’‘c’+‘ba’ ‘dcba’‘d’+‘cba’ 每個字母與之前的字串合併 把輸入的句子由尾反轉顯示 從第一個字母到最後一個字母

17 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=8 9-2=7 9-3=6 9-4=5 9-5=4 9-6=3 9-7=2 9-8=1

18 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. 輸入一個字母 檢查字母是否在句子裏 一旦發現字母在句子裏 設定找到 開始時設定找不到

19 WorkSheet write a program that accepts a string and change each uppercase letter to lowercase letter. Enter your school name :I studied from Our Lady College The result is : i studied from our lady college Output: program uppertolowercase; var w: string; i : integer; begin writeln('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') then write(chr(ord(w[i])+32)) else write(w[i]) end. 測試句子內每一個字符 如果是英文字母大楷 顯示細楷 否則 顯示原本字符

20 WorkSheet write a program that accepts a string and change each lowercase letter to uppercase or vice versa. Enter your school name :50th Anniversary Our Lady College The result is : 50TH aNNIVERSARY oUR lADY cOLLEGE Output: program convertcase; var w: string; i : integer; begin writeln('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') then write(chr(ord(w[i])+32)) else if (w[i]>=‘a') and (w[i] <=‘z') then write(chr(ord(w[i])-32)) else write(w[i]) end. 測試句子內每一個字符 如果字符是英文字母大楷 顯示細楷 否則 如果是英文字母細楷 顯示大楷 否則 顯示原本字符

21 WorkSheet Output: Enter a sentence : Computer Information Technology The 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 Technology program 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. 測試句子內每一個字符 如果字符是韻母 將韻母數目加一

22 WorkSheet write a program that accepts a sentence and display the first letter of each word First Run: Enter your school name :OurLadyCollege Your school is : OLC Output: Second Run: Enter your school name :OurLadyCollege Your 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]) end end. 顯示句子內每一個字的首字母 如果這是空字符和下一個是有字符 顯示下一個字符 如果這是第一個字符而不是空字符 顯示這個字符 測試每一個句子內每一個字符

23 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.

24 program pattern1; var c : string; i : integer; begin c := ''; for i := 1 to 4 do begin c := c + '*'; writeln(c) end; end. * ** *** **** program pattern2; var c : string; i : integer; begin c := ''; for i := 1 to 4 do begin c := c + '*'; writeln(c:4) end; end. * ** *** **** c=‘’ c=c+’*’ ‘*’‘’+‘*’ ‘**’‘*’+ ‘*’ ‘***’‘**’+ ‘*’ ‘****’‘***’+‘*’

25 Nest-FOR program MultiplyTable; var I,J : integer; begin for I := 1 to 10 do begin for J := 1 to 10 do write(I * J :4); writeln end end. I J

26 Nest-FOR program pattern; var i,j : integer; begin for i := 1 to 4 do begin for j := 1 to i do write('*'); writeln end end. program pattern; var i,j : integer; begin for i := 4 downto 1 do begin for j := 1 to i do write('*'); writeln end end. ** *** **** 1 i 2 3 4 1 2 3 4 j **** **** * 4 i 3 2 1 4 3 2 1 j

27 Nest-FOR program pattern; var i,j : integer; begin for i := 1 to 4 do begin write('':4-i); for j := 1 to i do write('*'); writeln end end. 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 end end. ** *** ** 1 i 2 3 4 3 2 1 0 4-i * *** **** * 4 i 3 2 1 0 1 2 3 4-i

28 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(‘|’) END END. PROGRAM PATTERN; VAR I,J: INTEGER; BEGIN FOR I := 1 TO 3 DO BEGIN FOR J := 1 TO 4 DO WRITE(‘|---’); WRITELN(‘|’) END END. 連續顯示12次 |--- 每四次 落一行 顯示三行 每一行 顯示四次 |---

29 WorkSheet Q2 Denary Number = a  2 5 + b  2 3 +c  2 3 + d  2 2 + e  2 1 +f  2 0 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: 1 0 1 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.

30 Project Write a program that display the result of days for each month in the following format: Month 1 1234567 891011121314 15161718192021 22232425262728 293031 Output: Month 2 1234567 891011121314 15161718192021 22232425262728 29. Month 12 1234567 891011121314 15161718192021 22232425262728 293031

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 do begin write('Enter your marks '); readln(mark) end end. DIFFERENCE BETWEEN FOR AND REPEAT LOOP program 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.

32 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.

33 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.

34 REPEAT statement 1; statement2;. UNTIL condition; program enteryourage; var age : integer; begin repeat writeln(‘enter your age:’); readln(age); until age > 12; end. VALIDATION INPUT TECHNIQUE program enteryourage; var age : integer; begin repeat writeln(‘enter your age:’); readln(age); until (age > 12) and (age <15); end.

35 program atm; var key : integer; begin repeat writeln(‘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’) else writeln(‘please leave’) end. VALIDATION INPUT AND COUNTING TECHNIQUE

36 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=0 term=term+1 1=0+1 2=1+1 3=2+1 4=3+1 5=4+1 sum=0 sum=sum+term 1=0+1 3=1+2 6=3+3 10=6+4 15=10+5

37 program FindRemainder; var d, num : integer; begin num:=9; d :=2; repeat num := num - d; until num < d; writeln('the remainder is ', num); end. num=num-d 7=9-2 5=7-2 3=5-2 1=3-2

38 COUNTING AND ACCUMULATING TECHNIQUE program compoundinterest; var p : real; yr : integer; begin p:=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.

39 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.

40 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.

41 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.

42 program atm; varamt,money,choice,count : real; password: string; ans : char; begin count := 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.

43 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.

44 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.

45 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.

46 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.

47 program MarkSix; const max = 20; var a,b,c,d,e,f,g,h,i,j,k,l, count: integer; Begin repeat readln(g) until (g > 0) and (g <=max); repeat readln(h) until (h > 0) and (h g); repeat readln(i) until (i > 0) and (i g) and (i <> h); repeat readln(j) until (j > 0) and (j g) and (j<> h) and (j<> i); repeat readln(k) until (k > 0) and (k g) and (k<> h) and (k<> i) and (k<>j); repeat readln(l) until (l > 0) 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) then count := count+1; if (i=a) or (i=b) or (i=c) or (i=d) or (i=e) or (i=f) then count := count+1; if (j=a) or (j=b) or (j=c) or (j=d) or (j=e) or (j=f) then count := count+1; if (k=a) or (k=b) or (k=c) or (k=d) or (k=e) or (k=f) then count := count+1; if (l=a) or (l=b) or (l=c) or (l=d) or (l=e) or (l=f) then count := count+1; case count of 0..2 : writeln(count,' match.No price!'); 3 : writeln('fourth price'); 4: writeln('third price $20'); 5: writeln('second price'); 6: writeln('First price') end end.

48 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: 1 Correct. Your money so far is $1000 guess a number 1 or 2: 2 Correct. Your money so far is $2000 guess a number 1 or 2: 1 Wrong! The number generated is 2. Your money now is $2000 Output:

49 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? G Correct. 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? L Correct. 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? E Correct. 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? L Incorrect! The numbers are 1 and 2. You have gained 3000 points.

50 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.

51 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.

52 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.


Download ppt "FOR := to do Definite Iteration statements execute a statement repeatedly for a definite number of times where must be of integer, char, boolean."

Similar presentations


Ads by Google