Exercise 4.5
- What value(s) of the variable response will stop the following loops?
loop
exit when response = 'y' and response = 'Y'
.
.
.
end loop
loop
exit when response < 'A' and response > 'E'
.
.
.
end loop
- What output will be produced by each program?
var width : int := 0
for letter : 'C' .. 'H'
for i : 1 .. width
put " "..
end for
put letter
width := width + 1
end for
for decreasing i : 5 .. 1
for j : 0 .. 4
put i, j, " " ..
end for
put ""
end for
var x : int := 40
loop
exit when x <= 0
put x, " " ..
x := x - 7
end loop
- Rewrite each program with the appropriate indentation and spacing to correctly show its structure.
var count : int := 2 loop if count mod 10=0 then put""
end if put count," ", count**2 count := count + 2
exit when count > 40 end loop
var power : int for i:1..5 for j:0..4 power:=1
for k:1..j power:=power*i end for
put" ",i,"\^",j," ",power end for put"" end for
- what output will be produced by each program?
- What will the following program print given input of 540?
const divisor := 3
var number, test, count : int
get number
test := number
count := 0
loop
exit when test mod divisor not= 0
test := test div 3
count := count + 1
end loop
put number, " has ", count, " ", divisor, "'s"
- Describe in a few words the purpose of the program.
- Frequently, when computers are used to write cheques, the amounts are printed with leading asterisks in order to prevent fraud. For example, the value 27 might be printed as $***27, rather than $ 27. Modify the program shown in Exercise 4.2, Question 3 so that it has this effect.
- Write a program to read a natural number and then find and print all of its exact divisors.
- Write a program that takes as input the number of days in a month and the day of the week on which the first of the month occurs. The program should produce a display of the calendar for that month in standard form. For example, if there are thirty days in the month and the first day of the month is on a Wednesday, output should be similar to the following.
Sun Mon Tue Wed Thu Fri Sat
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
|
- A natural number, N, greater than one, is a prime number if it has no natural number divisors other than 1 and N. Write a program that finds the first five hundred primes, printing them ten to a line.
|
|