/ Published in: Pascal
Public domain.
Expand |
Embed | Plain Text
PROGRAM bindec; USES crt; VAR dec_begin, i: Integer; bin_number: String; zero_or_one, bin_or_dec: Char; bin: String; currHeading, intBinChar, x: Integer; dec: Real; repeat_char: Char; BEGIN clrscr; REPEAT WRITELN('To convert binary to decimal, press B; to convert decimal to binary, press D'); READLN(bin_or_dec); IF (bin_or_dec='D') OR (bin_or_dec='d') THEN BEGIN WRITELN('Please enter a decimal number to convert to binary: '); READLN(dec_begin); REPEAT IF (dec_begin mod 2) = 0 THEN zero_or_one := '0' ELSE zero_or_one := '1'; bin_number := bin_number + zero_or_one; dec_begin := dec_begin div 2; UNTIL dec_begin = 0; FOR i := Length(bin_number) DOWNTO 1 DO WRITE(bin_number[i]); WRITELN; END ELSE BEGIN WRITELN('Please enter a binary number to convert to decimal: '); READLN(bin); dec := 0; currHeading := 0; FOR i := Length(bin) DOWNTO 0 DO BEGIN IF currHeading > 0 THEN currHeading := currHeading * 2 ELSE currHeading := 1; Val(bin[i], intBinChar, x); dec := dec + (intBinChar * currHeading); END; WRITELN(dec:10:0); END; WRITELN('Repeat? Y/N'); READLN(repeat_char); UNTIL (repeat_char = 'N') OR (repeat_char = 'n'); READLN; END.
You need to login to post a comment.
