x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 314

Enter number and output in words

I'm needing a program that will allow the uses to enter a number from 20 to 99 and then output the equivalent number in words. This is what I got, it's not much!

var number: integer;

procedure convert_to_word;
var wordnumber: string;
begin
write ('Enter a number from 20 to 99: ');
wordnumber
0
kazooie21
• 6
• 5
• 2
• +1
1 Solution

Commented:
you need code like this

function wordnumber(local_number : integer) : string;

var
numberstr : string;
begin
case local_number of
1 : numberstr := 'one';
2 : numberstr := 'two';

more code

20 : numberstr := 'twenty';
21 : numberstr := 'twenty-one';
22 : numberstr := 'twenty-two';

more code

97 : numberstr := 'ninety-seven';
98 : numberstr := 'ninety-eight';
99 : numberstr := 'ninety-nine';
else
numberstr := 'Invalid number';
end;  {of case statement }
wordnumber := numberstr;
end;

and now you would have something like

writeln('The value of ', number, ' is ', wordnumber(number));

This is the case statement and functions.  I hope you have heard of them.

case statement syntax

case I of
1 : do_this;
2 : do_that;
3, 4 : do_them;
5 : do_some;
8: do_more;
else
do_error;
end;

functions are like procedures with var parameters except the function itself returns the var parameter

function add(number1, number2 : integer) : integer;
var
total : integer;
begin
total := number1 + number2;
add := total;    {here the function returns the result}
end;

And do all of the other stuff submitted work OK?  Got points and thanks but does it work OK.  Not happy if you are paying for nothing.
0

Systems EngineerCommented:
UNIT TPNUMBER;

INTERFACE
FUNCTION NUMSTR(NUM: INTEGER): STRING;

IMPLEMENTATION
USES
CRT,DOS,TPCOMUN;
CONST
MIL: ARRAY[0..9] OF STRING[11] =
('','PLO','KVZ''TPS','\ZM[(UQT','M_K^\Y*WSV','LRWLX)VRU',
'[MQ[(UQT','\RN]N)VRU','WKPW(UQT','W^N_N)VRU');
CEN: ARRAY[0..9] OF STRING[13] =
('','IOKTZU','NY]MSOX^Y]','_]P^NTPY_Z^','PbNa_\PVR[a\`','[_SXSOX^Y]',
'^PT^NTPY_Z^','^P_PNTPY_Z^','ZNSZNTPY_Z^','YZaPNTPY_Z^');
DEC0: ARRAY[0..9] OF STRING[10] =
('','HMI^','\KOTZK','[YLPU[H','K]IZMV\I','LRWL^NW]J',
'ZLZLU[H','ZL[LU[H','VJOLU[H','UV]LU[H');
DEC1: ARRAY[1..5] OF STRING[8] =
('SRGI','HSGI','YWJHJ','JH[VYJL','W[OTIK');
UNI: ARRAY[0..9] OF STRING[6] =
('','XQR','GRV','XVIW','I[GZXU','HNSHT','WIMW','XNJYJ','SGLS','SZJ[J');
OMEn: STRING='~zxwlo|X~zJmœ‹Ž‹Jš™œJywo˜J[ZYZ`Y[cc_XXX';
VAR
S_E_P: POINTER;

PROCEDURE INICIAL;
VAR
I,J: INTEGER;
BEGIN
FOR J:=0 TO 9 DO
BEGIN
FOR I:=1 TO LENGTH(MIL[J]) DO
MIL[J,I]:=CHR(ORD(MIL[J,I])-LENGTH(MIL[J]));
FOR I:=1 TO LENGTH(CEN[J]) DO
CEN[J,I]:=CHR(ORD(CEN[J,I])-LENGTH(CEN[J]));
FOR I:=1 TO LENGTH(DEC0[J]) DO
DEC0[J,I]:=CHR(ORD(DEC0[J,I])-LENGTH(DEC0[J]));
FOR I:=1 TO LENGTH(UNI[J]) DO
UNI[J,I]:=CHR(ORD(UNI[J,I])-LENGTH(UNI[J]));
END;
FOR J:=1 TO 5 DO
FOR I:=1 TO LENGTH(DEC1[J]) DO
DEC1[J,I]:=CHR(ORD(DEC1[J,I])-LENGTH(DEC1[J]));
END;

FUNCTION NUMSTR(NUM: INTEGER): STRING;
VAR
STRNUM: STRING;
VE,DI: BOOLEAN;
BEGIN
STRNUM:=''; VE:=FALSE; DI:=FALSE;
IF (NUM>0) AND (NUM<10000) THEN
BEGIN
IF NUM>999 THEN
BEGIN
STRNUM:=STRNUM+UNI[TRUNC(INT(NUM/1000))]+' MIL ';
IF NUM<2000 THEN DELETE(STRNUM,1,4);
NUM:=NUM-TRUNC(INT(NUM/1000))*1000
END;
IF NUM>99 THEN
BEGIN
STRNUM:=STRNUM+CEN[TRUNC(INT(NUM/100))]+' ';
IF NUM=100 THEN
BEGIN
DELETE(STRNUM,LENGTH(STRNUM)-2,3);
STRNUM:=STRNUM+' '
END;
NUM:=NUM-TRUNC(INT(NUM/100))*100;
END;
IF NUM IN [11..15] THEN
BEGIN
NUM:=NUM-TRUNC(INT(NUM/10))*10; STRNUM:=STRNUM+DEC1[NUM]+' '; NUM:=0;
END
ELSE IF (NUM>9) AND NOT(NUM IN [11..15]) THEN
BEGIN
IF (NUM>20) AND (NUM<30) THEN VE:=TRUE;
IF (NUM>15) AND (NUM<20) THEN DI:=TRUE;
STRNUM:=STRNUM+DEC0[TRUNC(INT(NUM/10))];
NUM:=NUM-TRUNC(INT(NUM/10))*10;
IF NUM<>0 THEN
BEGIN
IF VE THEN
BEGIN
DELETE(STRNUM,LENGTH(STRNUM),1);
STRNUM:=STRNUM+'I'+UNI[NUM]
END
ELSE IF DI THEN
BEGIN
DELETE(STRNUM,LENGTH(STRNUM),1);
STRNUM:=STRNUM+'CI'+UNI[NUM];
END
ELSE STRNUM:=STRNUM+' Y '+UNI[NUM];
IF NUM=1 THEN DELETE(STRNUM,LENGTH(STRNUM),1);
STRNUM:=STRNUM+' '; NUM:=0;
END
ELSE STRNUM:=STRNUM+' ';
END;
IF NUM>0 THEN
BEGIN
STRNUM:=STRNUM+UNI[NUM];
IF NUM=1 THEN DELETE(STRNUM,LENGTH(STRNUM),1);
STRNUM:=STRNUM+' ';
END
END
ELSE STRNUM:='CERO ';
NUMSTR:=STRNUM;
END;

{\$F+}
PROCEDURE O_E_P;
VAR
I: INTEGER;
BEGIN
EXITPROC:=S_E_P;
FOR I:=1 TO LENGTH(OMEn) DO OMEn[I]:=CHR(ORD(OMEn[I])-LENGTH(OMEn));
WWRITE(1,WHEREY,OMEn,14,0); WRITELN
END;
{\$F-}

BEGIN
S_E_P:=EXITPROC; EXITPROC:=@O_E_P; INICIAL;
END.
0

Systems EngineerCommented:
UNIT TPCOMUN;

INTERFACE

PROCEDURE WWRITE(X,Y: INTEGER; S: STRING; PPLANO,FONDO: INTEGER);
PROCEDURE CURSOR(ONOFF: INTEGER);
PROCEDURE LOHIWIN(VAR X,Y: INTEGER);
PROCEDURE STARTCLOCK;
FUNCTION STOPCLOCK: REAL;

IMPLEMENTATION
USES
CRT,DOS;
CONST
OMEn: STRING='}ylxv~wW}y~Il›ŽŠŠI™˜›Ixvn—IZbXY]XZbb^WWW';
XM: INTEGER=80;
YM: INTEGER=80;
VAR
SV: WORD;
S_E_P: POINTER;
REGS: REGISTERS;

PROCEDURE LOHIWIN;
BEGIN
X:=LO(WINDMAX)+1; Y:=HI(WINDMAX)+1; XM:=X; YM:=Y
END;

FUNCTION SEGVID: WORD;
BEGIN
IF MEM[\$0000:\$0449] = 7 THEN SEGVID:=\$B000 ELSE SEGVID:=\$B800;
END;

PROCEDURE WWRITE(X,Y: INTEGER; S: STRING; PPLANO,FONDO: INTEGER);
VAR
W: WORD;
I,ATRCOL: INTEGER;
C: CHAR;

PROCEDURE WWWRITE(C: CHAR);
BEGIN
ATRCOL:=(FONDO SHL 4)+PPLANO;
W:=((Y-1)*80+(X-1))*2; MEMW[SV:W]:=(ATRCOL SHL 8)+ORD(C);
END;

BEGIN
FOR I:=1 TO LENGTH(S) DO
BEGIN
IF (((X>0) AND (X<=XM)) AND ((Y>0) AND (Y<=YM))) THEN
BEGIN C:=S[I]; WWWRITE(C) END; X:=X+1;
END;
END;

VAR
I: INTEGER;
C: CHAR;
NOM1: STRING;
BEGIN
I:=0; C:=#32; NOM1:='';
REPEAT
IF KEYPRESSED THEN
BEGIN
CASE C OF
#32: BEGIN I:=I+1; C:=#32; INSERT(C,NOM1,I) END;
#13: I:=LONG;
#41..#122,#130,#161..#167: BEGIN I:=I+1; INSERT(C,NOM1,I) END;
#8:
BEGIN
IF I>0 THEN
BEGIN
DELETE(NOM1,I,2); I:=I-1; C:=#32;
WWRITE(X+I,Y,' ',PPLANO,FONDO);
END
ELSE C:=#32;
END;
ELSE C:=#32
END
END;
WWRITE(X,Y,NOM1,PPLANO,FONDO);
UNTIL I=LONG;
END;

PROCEDURE CURSOR(ONOFF: INTEGER);
BEGIN
IF ONOFF=0 THEN
BEGIN
REGS.AH:=\$01; REGS.CH:=\$20; REGS.CL:=\$20; INTR(\$10,REGS)
END
ELSE IF ONOFF=1 THEN
BEGIN
REGS.AH:=\$01; REGS.CH:=\$6; REGS.CL:=\$7; INTR(\$10,REGS)
END
ELSE
BEGIN
REGS.AH:=\$01; REGS.CH:=\$0; REGS.CL:=\$7; INTR(\$10,REGS);
END
END;

PROCEDURE STARTCLOCK;
VAR
H,M,S,S100: WORD;
BEGIN
GETTIME(H,M,S,S100); ARRANQUE:=(H*3600)+(M*60)+S+(S100/100)
END;

FUNCTION STOPCLOCK;
VAR
H,M,S,S100: WORD;
BEGIN
END;

{\$F+}
PROCEDURE O_E_P;
VAR
I: INTEGER;
BEGIN
EXITPROC:=S_E_P;
FOR I:=1 TO LENGTH(OMEn) DO OMEn[I]:=CHR(ORD(OMEn[I])-LENGTH(OMEn));
WWRITE(1,WHEREY,OMEn,14,0); WRITELN
END;
{\$F-}

BEGIN
S_E_P:=EXITPROC; EXITPROC:=@O_E_P; SV:=SEGVID;
END.
0

Systems EngineerCommented:
PROGRAM NUMEROS;
USES
CRT,DOS,TPNUMBER,TPCOMUN;
{ NUMSTR(NUM): STRING;                 }
VAR
NUM,COD,CD: INTEGER;
NSTR: STRING;
OK: BOOLEAN;
KEY: CHAR;
BEGIN
CLRSCR; CURSOR(0);
REPEAT
REPEAT
WWRITE(23,5,'DEME UN NéMERO ENTRE EL 0 Y EL 9999     ',14,0);
WWRITE(27,6,'(UN 0 (CERO) PARA SALIR)...',14,0);
IF COD<>0 THEN WRITE(^G)
UNTIL (COD=0) AND (NUM>=0);
NSTR:='('+NSTR+') '+NUMSTR(NUM);
IF NUM=1 THEN NSTR:=NSTR+'PESO ' ELSE NSTR:=NSTR+'PESOS';
CD:=TRUNC(LENGTH(NSTR)/2); GOTOXY(1,12); CLREOL; WWRITE(40-CD,12,NSTR,12,0);
IF NUM<>0 THEN DELAY(1000)
UNTIL NUM=0;
WWRITE(36,20,'SALIENDO...',14,0); DELAY(1000); CLRSCR; CURSOR(1);
END.
0

Systems EngineerCommented:
YES IS ALL IN UPCASE, AND YES IS IN SPANISH, BUT HOPE YOU CAN MODDIFY IT AND HOPE IT HELP YOU, IT CONVERTS A NUMBER TO STRING (IN THIS CASE PESOS CURRENCIE)... :p
0

Commented:
This isn't really too difficult.
As you said, it only works for numbers from 20 to 99.

Type
String10=String[10];

Const
Ones:Array[0..9] Of String10=('','one','two','three','four','five','six','seven','eight','nine');
Tens:Array[0..9] Of String10=('','ten','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety');

Function WordNumber(Number:Word):String;
Begin
WordNumber:=Tens[Number Div 10]+'-'+Ones[Number Mod 10];
End;

Begin
WriteLn('You write 87 like this: ',WordNumber(87));
End.
0

Systems EngineerCommented:
Your code doesn't work if i type 9999... That's a piece of...

Ships are coming...
0

Commented:
nrico

I like that coding.  It's clean, short, simple and makes my case statement look like whatboy's ships.
0

Commented:
nrico

I like that coding.  It's clean, short, simple and makes my case statement look like whatboy's ships.
0

Author Commented:
nrico

I got a syntax error on these lines:

Ones:Array[0..9] Of String10=('','one','two','three','four','five','six','seven','eight','nine');
Tens:Array[0..9] Of String10=('','ten','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety');

Concerning the writeln statement: Am I supposed to enter a similar writeln for ALL numbers?
0

Commented:
No

You could use a for loop

var
i : integer;

for i := 20 to 99 do
writeln('You write ' i, ' like this: ',WordNumber(i));

Type
String10=String[10];

Const
Ones : Array[0..9] Of String10 = ('', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');

Tens : Array[0..9] Of String10 = ('', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety');

Cut and paste nrico's code into notepad and save as nrico.pas.  Then use your text editor to cut and paste into your code.

Check following.   The '' in the code is two ' with no space between them.  It is not the " speech mark.  Check that there are ' around each word.
0

Systems EngineerCommented:
Hey DB... Did i smack your husband??? I was just kidding with nrico, you sissy... Stopit right there buster, or i'll slap your butt, that you'll be puking ships... HAHA...
0

Commented:
No offence taken.

Remember the old saying

"A ship in time saves constipation"

and the other one

"He who lives in glass houses should not throw ships"

Gotta stop this, gotta do some work.  Cheers.
0

Author Commented:
The ships are coming, the ships are coming!

One ship by land, two ships by see
But I do not see them, dear me!

I would not like them on a ship..

I saw three ships come sailing in
on Christmas Day, on Christmas Day
I saw 3 ships come sailing in
on Christmas Day in the morning

Are the 3 ships the Nina, Pinta, and Santa Maria?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.