1 / 52

FOR <Control Variable> := <Initial Value> to <Final Value> do <Loop statement>

Definite Iteration statements execute a statement repeatedly for a definite number of times. FOR <Control Variable> := <Initial Value> to <Final Value> do <Loop statement>. where <Control Variable> must be of integer, char, boolean. Set I to 1. Set I to ‘A’. F. I <= 5?. T.

alaula
Download Presentation

FOR <Control Variable> := <Initial Value> to <Final Value> do <Loop statement>

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Definite Iteration statements execute a statement repeatedly for a definite number of times FOR <Control Variable> := <Initial Value> to <Final Value> do <Loop statement> where <Control Variable> must be of integer, char, boolean

  2. Set I to 1 Set I to ‘A’ F I <= 5? T F I <= ‘E’? Display I T T Display I Increment I by 1 T Increment I by 1 character 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.

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

  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. 3 a:= 1 a:= a  2 212422842 i=1 i=2 i=3 不斷將2自乘p次 23=8 p=3 執行p次 將2自乘

  7. WorkSheet • Write a program that • accepts an integer N • And calculate the result of 1  2  3 …….N 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. Output: Enter a number : 5 The result of 1  2  ……. 5 is: 120 執行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 A 1000 A:= A  (1+R/100) 11001000 1.112101100 1.1133112101.1 I=1 I=2 I=3 P=1000 R=10 N=3 執行N次 將 本金不斷乘以(1+R/100)

  9. Example 5.2 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. 如果是單數(不可除盡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. N mod i = 0 24 mod 1 = 0 24 mod 2 = 0 24 mod 3 = 0 24 mod 4 = 0 24 mod 6 = 0 24 mod 8 = 0 24 mod 12= 0 24 mod 24= 0 找出某數的所有因數 24 24 如 數值如24可以除盡 I 的話, 即I是24的因數), 顯示因數 Output:The factors of 24 are 1 2 3 4 6 8 12 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. 如果 I可以除盡7的話(即 I是7的倍數) 便落一行 Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

  13. 3 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. count = 1 3 mod 1 = 0 count = 2 3 mod 3 = 0 6 count = 1 6 mod 1 = 0 count = 2 6 mod 2 = 0 count = 3 6 mod 3 = 0 count = 4 6 mod 6 = 0 如 數值如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. 找出最少公因數 9 63 L=9 9 mod 3=0 63 mod 3=0 那個數目最少用L載著 找出兩個數的公因數I

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

  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 從第一個字母到最後一個字母 c w[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. Output: Enter your school name :I studied from Our Lady CollegeThe result is : i studied from our lady college 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. Output: Enter your school name :50th Anniversary Our Lady CollegeThe result is : 50TH aNNIVERSARYoUR lADY cOLLEGE 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 • Write a program that • accepts a sentence • And calculate the number of occurrences for the vowels. Output: Enter a sentence : Computer Information Technology The number of occurrence for the vowels :10 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 顯示句子內每一個字的首字母 Output: First Run: Enter your school name :OurLadyCollegeYour school is : OLC Second Run: Enter your school name :OurLadyCollegeYour 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. X 1 2 3 4 5 6 7 8 9 10 1 1 2 3 4 5 6 7 8 9 10 2 2 4 6 8 10 12 14 16 18 20 3 3 6 9 12 15 18 21 24 27 30 4 4 8 12 16 20 24 28 32 36 40 5 5 10 15 20 25 30 35 40 45 50 6 6 12 18 24 30 36 42 48 54 60 7 7 14 21 28 35 42 49 56 63 70 8 8 16 24 32 40 48 56 64 72 80 9 9 18 27 36 45 54 63 72 81 90 10 10 20 30 40 50 60 70 80 90 100 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. J I

  26. j i j i *  * * * * * * ** * 1 1 * * *** * * * * * 4 4 2 2 3 3 3 3 2 2 4 4 1 1 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.

  27. 4-i i 4-i i 0  *  * * * * * * * * * 1 3 4 1 2 3 2 3 2 2 1 4 0 1 3 Nest-FOR 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. 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. * * * * * * * * *  *

  28. WorkSheet Q1 Using for statement with if-statement within , write a program that generates the following output: PROGRAM PATTERN; VAR I : INTEGER; BEGIN FOR I := 1 TO 12 DO BEGIN WRITE(‘|---’); IF I MOD 4 = 0 THEN WRITELN(‘|’) END END. |---|---|---|---| |---|---|---|---| |---|---|---|---| 連續顯示12次|--- 每四次落一行 WorkSheet Q2 Using nested-for , write a program that generates the following output: |---|---|---|---| |---|---|---|---| |---|---|---|---| PROGRAM PATTERN; VAR I ,J: INTEGER; BEGIN FOR I := 1 TO 3 DO BEGIN FOR J := 1 TO 4 DO WRITE(‘|---’); WRITELN(‘|’) END END. 顯示三行 每一行顯示四次 |---

  29. WorkSheet Q2 Denary Number = a25 + b23 +c23 + d22 + e21+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. 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. Output: Enter binary number: 1 0 1 1 1 The denary number is 23

  30. Project • Write a program that • display the result of days for each month in the following format: Output: Month 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Month 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 . . . Month 12 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

  31. DIFFERENCE BETWEEN FOR AND REPEAT LOOP program Ex1; var i : integer; Begin i := 0; repeat i := i + 1; writeln(i); untili = 5; end. 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. 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. program enteryourage; var age : integer; begin repeat writeln(‘enter your age:’); readln(age); until age > 12; end. REPEAT statement 1; statement2; . . UNTILcondition; VALIDATION INPUT TECHNIQUE program enteryourage; var age : integer; begin repeat writeln(‘enter your age:’); readln(age); until (age > 12) and (age <15); end.

  35. VALIDATION INPUT TECHNIQUE VALIDATION INPUT AND COUNTING 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. program atm; var key : integer; begin repeat writeln(‘enter password:’); readln(key); until key = 12345; end.

  36. COUNTING AND ACCUMULATING 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. term=0 term =term +11 =0 +12 =1 +1 3 =2 +1 4 =3 +1 5 =4 +1 sum=0 sum =sum+term1 =0 +13 =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; var amt,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. 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. 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 <=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);

  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. Output: 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

  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.

More Related