Friday, December 18, 2020

Pascal Program Exercises with Answers (Binary, Decimal Conversions)

Write a Pascal Program to convert a given Decimal number to equivalent Binary number?

 program DecimalToBinary;

var
  remainder, quotient, num,i:integer;
  binary,addbin:String;

begin
  write('Enter a Decimal Number?');
  read(num);
  addbin:='';
  binary:='';
  while (num >=2)  do
  begin
    remainder:= num mod 2;
    quotient:= num div 2;
    Str(remainder,binary);
    addbin:=addbin+binary;
    num:= quotient ;
  end;
  Str(num,binary);
  addbin:=addbin+binary;
  for i:=length(addbin) downto 1 do
  begin
      write(addbin[i]);
  end;
  readln;
end.  


Write a Pascal Program to convert a given Binary number to equivalent Decimal number?

 program BinarytoDecimal;
uses math,sysutils;

var
   binary:string;
   i,j,num:integer;
   decnum:float;

begin
  decnum:=0;
  write('Enter a Binary number ?');
  read(binary);
  write(chr(13));
  write('Binary number :');
  writeln(binary);
  write(chr(13));
  i:=length(binary);
  j:=0;
  while (i >=1) do
  begin
    num:=StrToInt(binary[i]) ;
    decnum:=decnum+(num * power(2,j));
    i:=i-1;
    j:=j+1;
  end;
  write('Equivalent decimal number is :');
  writeln(decnum:8:0);
  readln;
  readln;
end.
            
Pascal Program to Convert Binary to Decimal


No comments:

Post a Comment