Saturday, September 10, 2016

Decimal Number to Binary Equivalent

Write  a  Program  to  convert  a  Decimal  number  to  its  Binary  equivalent .

Program Coding    : :



Program  Binary_Number
 Implicit  none
 Integer :: i_part , rem , i_base
 Real ( kind=8 ) :: Bina_num ,  Deci_num ,  r_part , r_base , ir_part
 21 Print *, "Decimal Number :"
 Read (*,*) Deci_num
 Bina_num = 0.0 !For Integer Part of Deci_num equivalent Binary
 i_base = 1
 i_part = INT(Deci_num)

 Do while (i_part>0)
     rem = Mod(i_part, 2)
     Bina_num = Bina_num + rem*i_base
   i_base = i_base*10
   i_part = Int(i_part/2)
 end do           !For Real Part of Deci_num equivalent Binary

r_base = 0.1
 r_part = Deci_num -INT(Deci_num) !Real part of Decimal number

Do while(r_part > 0)
 r_part = r_part * 2
 ir_part = Int(r_part)     !Integer part of Real_part*2
 Bina_num = Bina_num  +  ir_part * r_base
 r_base = r_base / 10.0
 r_part = r_part - ir_part      !Again Real part is only decimal value
 End do

 Write(*,121) "Binary Number :"  ,  Bina_num
 121 format(2x, A15, f20.6 , / , / )
 Goto 21
 End program

Input  : :




Output  ::




No comments:

Post a Comment