#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Pascal Programming

Posts: 2Member
Help with this program.

program binarytodecimal;

uses crt,strings;

type
bstring = array[0..80] of Char;

var b1,b2:bstring;
op:char;

{[*************]}

function IntToStr(I: Longint): String;
{ Convert any integer type to a string }
var
S: string;
begin
Str(I, S);
IntToStr := S;
end;

function tobinary(pass_d:longint):string;
{converts the decimal value to a binary}
var tempbinary:string;
remainder: integer;

begin
remainder:=pass_d; {set the value of the remainder}
tempbinary:=''; {make the temp space for the binary empty}

while (remainder > 0) do
begin
{make the binary string}
tempbinary := inttostr(remainder mod 2)+tempbinary;
{get a new remainder}
remainder:=remainder div 2;
end;
tobinary:=tempbinary;
end;

function raisetopower(n,p:integer):integer;
{function is used to raise n to the power of p}
var
times,ans:integer;
begin
ans:=1;
for times:=1 to p do
ans:=ans*n;
raisetopower:=ans;
end;

function todecimal(pass_b:bstring):integer;
{function used to convert a binary string
to a decimal value}
var total, {total decimal value for string}
l, {lenght of the string}
x:integer; {used to traverse the string array}

begin
total:=0; {reset to 0}
l:=strlen(pass_b); {get the lenght}

for x:=0 to l-1 do {from the right side stat calc.}
begin
if(pass_b[x] ='0')or (pass_b[x] = '1') then
begin
if pass_b[x]='1' then
total:=total+raisetopower(2,l-1-x); {calculate the decimal
value}
end
else
begin
Writeln;
writeln('Invalid Binary String!!!');
writeln('Program halted');
Halt;
end;

end;
todecimal:=total;
end;

begin
clrscr;
{sample main block}
write('Please enter the first binary number: ');
write('Please enter the secon binary number: ');
write('PRESS one of these keys [*] [/] [+] [-]');

'*':
writeln;
Write('ANS = ');
case op of
'-': writeln(tobinary(todecimal(b1)-todecimal(b2))); {subraction}
'/': writeln(tobinary(todecimal(b1)div todecimal(b2))); {division using div}
'*': writeln(tobinary(todecimal(b1)*todecimal(b2))); {multiplication}
else
begin
writeln('Invald Input!! Program Halted !!');
halt;
end;
end;

end.

· ·

• Posts: 6,349Member
: Help with this program.
:
:
:
: program binarytodecimal;
:
: uses crt,strings;
:
: type
: bstring = array[0..80] of Char;
:
:
: var b1,b2:bstring;
: op:char;
:
:
: {[*************]}
:
: function IntToStr(I: Longint): String;
: { Convert any integer type to a string }
: var
: S: string;
: begin
: Str(I, S);
: IntToStr := S;
: end;
:
: function tobinary(pass_d:longint):string;
: {converts the decimal value to a binary}
: var tempbinary:string;
: remainder: integer;
:
: begin
: remainder:=pass_d; {set the value of the remainder}
: tempbinary:=''; {make the temp space for the binary empty}
:
: while (remainder > 0) do
: begin
: {make the binary string}
: tempbinary := inttostr(remainder mod 2)+tempbinary;
: {get a new remainder}
: remainder:=remainder div 2;
: end;
: tobinary:=tempbinary;
: end;
:
: function raisetopower(n,p:integer):integer;
: {function is used to raise n to the power of p}
: var
: times,ans:integer;
: begin
: ans:=1;
: for times:=1 to p do
: ans:=ans*n;
: raisetopower:=ans;
: end;
:
:
: function todecimal(pass_b:bstring):integer;
: {function used to convert a binary string
: to a decimal value}
: var total, {total decimal value for string}
: l, {lenght of the string}
: x:integer; {used to traverse the string array}
:
: begin
: total:=0; {reset to 0}
: l:=strlen(pass_b); {get the lenght}
:
: for x:=0 to l-1 do {from the right side stat calc.}
: begin
: if(pass_b[x] ='0')or (pass_b[x] = '1') then
: begin
: if pass_b[x]='1' then
: total:=total+raisetopower(2,l-1-x); {calculate the decimal
: value}
: end
: else
: begin
: Writeln;
: writeln('Invalid Binary String!!!');
: writeln('Program halted');
: Halt;
: end;
:
: end;
: todecimal:=total;
: end;
:
: begin
: clrscr;
: {sample main block}
: write('Please enter the first binary number: ');
: write('Please enter the secon binary number: ');
: write('PRESS one of these keys [*] [/] [+] [-]');
:
:
: '*':
: writeln;
: Write('ANS = ');
: case op of
: '-': writeln(tobinary(todecimal(b1)-todecimal(b2))); {subraction}
: '/': writeln(tobinary(todecimal(b1)div todecimal(b2))); {division using div}
: '*': writeln(tobinary(todecimal(b1)*todecimal(b2))); {multiplication}
: else
: begin
: writeln('Invald Input!! Program Halted !!');
: halt;
: end;
: end;
:
:
:
: end.
:
:
First what is wrong with it? Second use style codes (See below message input screen for instructions)! Third read Raymond's "How to ask questions the smart way" guide.
· ·
• Posts: 349Member
: Help with this program.
:
:
:
: program binarytodecimal;
:
: uses crt,strings;
:
: type
: bstring = array[0..80] of Char;
:
:
: var b1,b2:bstring;
: op:char;
:
:
: {[*************]}
:
: function IntToStr(I: Longint): String;
: { Convert any integer type to a string }
: var
: S: string;
: begin
: Str(I, S);
: IntToStr := S;
: end;
:
: function tobinary(pass_d:longint):string;
: {converts the decimal value to a binary}
: var tempbinary:string;
: remainder: integer;
:
: begin
: remainder:=pass_d; {set the value of the remainder}
: tempbinary:=''; {make the temp space for the binary empty}
:
: while (remainder > 0) do
: begin
: {make the binary string}
: tempbinary := inttostr(remainder mod 2)+tempbinary;
: {get a new remainder}
: remainder:=remainder div 2;
: end;
: tobinary:=tempbinary;
: end;
:
: function raisetopower(n,p:integer):integer;
: {function is used to raise n to the power of p}
: var
: times,ans:integer;
: begin
: ans:=1;
: for times:=1 to p do
: ans:=ans*n;
: raisetopower:=ans;
: end;
:
:
: function todecimal(pass_b:bstring):integer;
: {function used to convert a binary string
: to a decimal value}
: var total, {total decimal value for string}
: l, {lenght of the string}
: x:integer; {used to traverse the string array}
:
: begin
: total:=0; {reset to 0}
: l:=strlen(pass_b); {get the lenght}
:
: for x:=0 to l-1 do {from the right side stat calc.}
: begin
: if(pass_b[x] ='0')or (pass_b[x] = '1') then
: begin
: if pass_b[x]='1' then
: total:=total+raisetopower(2,l-1-x); {calculate the decimal
: value}
: end
: else
: begin
: Writeln;
: writeln('Invalid Binary String!!!');
: writeln('Program halted');
: Halt;
: end;
:
: end;
: todecimal:=total;
: end;
:
: begin
: clrscr;
: {sample main block}
: write('Please enter the first binary number: ');
: write('Please enter the secon binary number: ');
: write('PRESS one of these keys [*] [/] [+] [-]');
:
:
: [red]'*': [/red]
: writeln;
: Write('ANS = ');
: case op of
: '-': writeln(tobinary(todecimal(b1)-todecimal(b2))); {subraction}
: '/': writeln(tobinary(todecimal(b1)div todecimal(b2))); {division using div}
: '*': writeln(tobinary(todecimal(b1)*todecimal(b2))); {multiplication}
: else
: begin
: writeln('Invald Input!! Program Halted !!');
: halt;
: end;
: end;
:
: