Programs to Complete: Naming Integers

Program NameInteger inputs an integer from 0 to 999 and displays the number in words. This would be useful for someone learning English. You could extend the program so that it:

  • names an integer from 0 to 999999;
  • gives the user a choice of languages for the words.
program NameInteger;
{$APPTYPE CONSOLE}
{Names a positive integer less than 1000.  Adapted from
"A students' Guide to Programming in Pascal", Lawrence V. Atkinson,
John Wiley & Sons, 1982.}
uses
  SysUtils;
var
  Int : integer;

procedure NameUnit (Digit : integer);
begin
  case Digit of
    0 : ;
    1 : write('one ');
    2 : write('two ');
    3 : write('three ');
    4 : write('four ');
    5 : write('five ');
    6 : write('six ');
    7 : write('seven ');
    8 : write('eight ');
    9 : write('nine ');
  end;
end;

procedure NameTeen (Teen : integer);
begin
  case Teen of
    10 : write('ten ');
    11 : write('eleven ');
    12 : write('twelve ');
    13 : write('thirteen ');
    14 : write('fourteen ');
    15 : write('fifteen ');
    16 : write('sixteen ');
    17 : write('seventeen ');
    18 : write('eighteen ');
    19 : write('nineteen ');
  end;
end;

procedure NameTy (Tens : integer);
begin
  case tens of
    2 : write('twenty ');
    3 : write('thirty ');
    4 : write('forty ');
    5 : write('fifty ');
    6 : write('sixty ');
    7 : write('seventy ');
    8 : write('eighty ');
    9 : write('ninety ');
  end;
end;

procedure NameLessThan100 (Num : integer; AndNeeded : Boolean);
begin
  if (Num <> 0) AND AndNeeded then
    write('and ');
  if Num < 10 then
    NameUnit (Num)
  else
    if Num < 20 then
      NameTeen (Num)
    else
      begin
        NameTy (Num DIV 10);
        NameUnit(Num MOD 10);
      end;
end;

procedure NameLessThan1000 (Number : integer; AndNeeded : Boolean);
begin
  if Number < 100 then
     NameLessThan100(Number, AndNeeded)
  else
    begin
      NameUnit (Number DIV 100);
      write ('hundred ');
      NameLessThan100(Number MOD 100, True);
    end;
end;

begin
  write('Type a positive integer less than 1000. ');
  readln(Int);
  if Int = 0 then
    write('zero')
  else
    NameLessThan1000(Int, False);
  readln;
end.

Programming - a skill for life!

Unfinished programs for you to complete