• How to convert denary to binary in pascal?

    Amit Member
    program test;
    uses wincrt;
    var s:string;rem,divi,dn:real;
    begin
    clrscr;
    write('input denary number: ');
    readln(dn);
    divi:=(dn/2);
    rem:=(dn mod 2);
    s:=' ';
    while divi<>0 do
    
    s:=copy(dn,rem)+s;
    writeln(s);
    readkey;
    end.
    

    please debug. How to convert denary to binary in pascal?

  • Amit Member
    program Project1;
    
    {$APPTYPE CONSOLE}
    
    uses
      SysUtils;
    
    const
      po2: array[0..7] of byte = (1,2,4,8,16,32,64,128); {powers of 2}
      bin: array[false..true] of char = '01';
    
    {Decimal to binary}
    function dec2bin(b:byte):string;
    var
      s: string;
      i: byte;
    begin
    
      s:='';
    
        for i := 7 downto 0 do
          s := s+bin[(po2[i] and b=po2[i])];
    
      dec2bin:=s;
    
    end;
    
    var
      n: byte;
    
    begin
    
      write(#13#10,'Enter a decimal :');
      readln(n);
      writeln(n, ' in binary :', dec2bin(n));
    
      readln;
    
    end.
    
Viewing 1 reply thread
  • You must be logged in to reply to this topic.