Algorithms and Programming eXercises #8
Iteration using WHILE..DO - Solutions
1. Procedure Tform1.getaLine;
{gets a line of input from the user and reports the number of characters typed}
Var thing : string;
tally : integer;
temp : string;
Begin
tally := 0;
thing := inputbox('Entry','Type a letter [terminate with 0]','');
while thing[1] <> '0' do
begin
inc(tally);
thing := inputbox('Entry','Type a letter [terminate with 0]','');
end;
temp := 'the number of characters typed was ' + inttostr(tally);
showmessage(temp)
End;
2. Procedure Tform1.LetterTypeCounter;
{gets line of characters, tallying number of vowels, consonants and spaces}
Var thing : char;
spaces,vowels,consonants : integer;
Begin
tally := 0;
thing := inputbox('Entry','Type a letter [terminate with 0]','');
while thing <> '0' do
if upcase(thing[1]) in [' ','A'..'Z'] then
case upcase(thing) of
' ' : inc(spaces);
'A','E',
'I','O','U' : inc(vowels)
else inc(consonants)
end; {case thing}
showmessage('there were ' + inttostr(consonants) + ' consonants typed');
showmessage('there were ' + inttostr(vowels) + ' consonants typed');
showmessage('there were ' + inttostr(spaces) + ' consonants typed');
End.
3. Program AsteriskBar;
{prompts for number and outputs that many asterisks}
Var num,
counter : byte;
Begin
write('How many asterisks do you want? :');
readln(num);
counter := 0;
while counter <> num do
begin
write('*');
inc(counter)
end
End.
4. {accepts a number and reports the power of two that just exceeds the number}
Var num,
temp : integer;
power : byte;
Begin
num := inttostr(inputbox('Data Entry','What number?',0);
power := 0;
temp := 1
while temp < num do
begin
temp := temp * 2;
inc(power)
end;
showmessage('the power of 2 just exceeding '+inttostr(num)+' is '+inttostr(power))
End;
5. Program Conversion;
Var I : integer;
Begin
I := 0;
while i*i <= 100 do
begin
showmessage(inttostr(i*i));
inc(i)
end
End.
|